Java Spring、Hibernate和AbstractTransactionalJUnit4SpringContextTests:找不到具有多个到相同基础数据的映射的对象
我已经使用为Spring2.5.6/Hibernate3.5.3项目成功创建了许多与数据库相关的单元测试。例如,创建设备对象,然后验证同一对象是否包含在所有设备对象的列表中:Java Spring、Hibernate和AbstractTransactionalJUnit4SpringContextTests:找不到具有多个到相同基础数据的映射的对象,java,spring,hibernate,junit,Java,Spring,Hibernate,Junit,我已经使用为Spring2.5.6/Hibernate3.5.3项目成功创建了许多与数据库相关的单元测试。例如,创建设备对象,然后验证同一对象是否包含在所有设备对象的列表中: @ContextConfiguration(locations = { "classpath:/UnitTests-context.xml" }) @RunWith(SpringJUnit4ClassRunner.class) public class DeviceDaoTests extends AbstractTran
@ContextConfiguration(locations = { "classpath:/UnitTests-context.xml" })
@RunWith(SpringJUnit4ClassRunner.class)
public class DeviceDaoTests extends AbstractTransactionalJUnit4SpringContextTests {
@Test
public void testGetDeviceList() {
Device device = new Device();
this.sessionFactory.getCurrentSession().merge(device);
Query myQuery = this.sessionFactory.getCurrentSession().createQuery("from Device");
List<Device> allDevices = myQuery.list();
assertTrue(allDevices.contains(device);
}
}
还有一个名为DeviceListItem的类,该类映射到名为DeviceList_view的数据库视图,该视图包含设备表(以及其他表)的一部分:
现在,如果我想测试在数据库中插入设备会导致从所有DeviceListItem对象的列表中找到具有相同id的DeviceListItem(与第一个测试类似),那么有一个问题-以下测试失败:
@Test
public void testGetDeviceListItemList() {
Device device = new Device();
this.sessionFactory.getCurrentSession().merge(device);
Query myQuery = this.sessionFactory.getCurrentSession().createQuery("from DeviceListItem");
List<DeviceListItem> allDeviceListItems = myQuery.list();
assertTrue(allDeviceListItems.get(0).getId().equals(device.getId())
}
@测试
public void testGetDeviceListItemList(){
设备=新设备();
this.sessionFactory.getCurrentSession().merge(设备);
Query myQuery=this.sessionFactory.getCurrentSession().createQuery(“来自DeviceListItem”);
List allDeviceListItems=myQuery.List();
assertTrue(allDeviceListItems.get(0).getId()等于(device.getId())
}
在web服务器上运行应用程序时,所有映射、DAO类等都会工作-只有前面提到的单元测试失败。这可能是因为保存了Device类型的新对象,但读取的项是DeviceListItem类型,Hibernate不“知道”它们都引用同一个底层数据库表,因为对象并没有真正保存到数据库中
是否有办法使此测试工作正常,即测试将设备对象写入数据库并作为DeviceListItem对象从数据库中读取的场景
(请注意,代码示例中可能存在打字错误、样式问题等,因为示例经过了高度简化,不一定是从实际执行的代码中复制/粘贴的)通过添加注释解决
@Rollback(false)
(参见)对该方法的修改
testGetDeviceListItemList
(参见原始问题)。然而,这也意味着Spring单元测试上下文不会自动回滚,因此我必须确保数据库状态在之后手动返回到正常状态:
this.sessionFactory.getCurrentSession().delete(device);
完整方法:
@Rollback(false)
@Test
public void testGetDeviceListItemList() {
Device device = new Device();
this.sessionFactory.getCurrentSession().merge(device);
Query myQuery = this.sessionFactory.getCurrentSession().createQuery("from DeviceListItem");
List<DeviceListItem> allDeviceListItems = myQuery.list();
assertTrue(allDeviceListItems.get(0).getId().equals(device.getId());
this.sessionFactory.getCurrentSession().delete(device);
}
@回滚(false)
@试验
public void testGetDeviceListItemList(){
设备=新设备();
this.sessionFactory.getCurrentSession().merge(设备);
Query myQuery=this.sessionFactory.getCurrentSession().createQuery(“来自DeviceListItem”);
List allDeviceListItems=myQuery.List();
assertTrue(allDeviceListItems.get(0.getId().equals)(device.getId());
this.sessionFactory.getCurrentSession().delete(设备);
}
您是否尝试过刷新或提交?只是猜测一下。我尝试过这两种方法,但结果相同——可能是因为扩展AbstractTransactionalJUnit4S普林gContextTests时,没有任何东西真正写入数据库。
this.sessionFactory.getCurrentSession().delete(device);
@Rollback(false)
@Test
public void testGetDeviceListItemList() {
Device device = new Device();
this.sessionFactory.getCurrentSession().merge(device);
Query myQuery = this.sessionFactory.getCurrentSession().createQuery("from DeviceListItem");
List<DeviceListItem> allDeviceListItems = myQuery.list();
assertTrue(allDeviceListItems.get(0).getId().equals(device.getId());
this.sessionFactory.getCurrentSession().delete(device);
}