让 DbUnit 与 Hibernate 事务一起工作
我在尝试将 Hibernate 事务中所做的更改推送到数据库以使 DbUnit 在我的测试用例中正常工作时遇到问题。看起来 DbUnit 没有看到 Hibernate 所做的更改,因为它们尚未在事务结束时提交......并且我不确定如何重组我的测试用例以使其正常工作。
这是我过度简化的测试用例来演示我的问题:-
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {
"classpath:applicationContext-test.xml"
})
@TransactionConfiguration(transactionManager = "transactionManager")
@Transactional
public class SomeTest {
@Autowired
protected DataSource dataSource;
@Autowired
private SessionFactory sessionFactory;
@Test
public void testThis() throws Exception {
Session session = sessionFactory.getCurrentSession();
assertEquals("initial overlayType count", 4, session.createQuery("from OverlayType").list().size());
//-----------
// Imagine this block is an API call, ex: someService.save("AAA");
// But for the sake of simplicity, I do it this way
OverlayType overlayType = new OverlayType();
overlayType.setName("AAA");
session.save(overlayType);
//-----------
// flush has no effect here
session.flush();
assertEquals("new overlayType count", 5, session.createQuery("from OverlayType").list().size());
// pull the data from database using dbunit
IDatabaseConnection connection = new DatabaseConnection(dataSource.getConnection());
connection.getConfig().setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY, new MySqlDataTypeFactory());
QueryDataSet partialDataSet = new QueryDataSet(connection);
partialDataSet.addTable("resultSet", "select * from overlayType");
ITable actualTable = partialDataSet.getTable("resultSet");
// FAIL: Actual row count is 4 instead of 5
assertEquals("dbunit's overlayType count", 5, actualTable.getRowCount());
DataSourceUtils.releaseConnection(connection.getConnection(), dataSource);
}
}
我使用 DbUnit 的整个想法是:-
- 调用将数据保存到多个表中的
someService.save(...)
。 - 使用 DbUnit 从 XML 中获取所需的表。
- 使用 DbUnit 从数据库获取实际表。
- 执行Assertion.assertEquals(expectedTable,actualTable);。
但是,此时,我无法让 DbUnit 查看 Hibernate 在事务中所做的更改。
我应该如何更改才能使 DbUnit 与 Hibernate 事务良好配合?
谢谢。
I'm having problem trying to push changes made within a Hibernate transaction to the database for DbUnit to work properly in my test case. It seems like DbUnit is not seeing the changes made by Hibernate because they are not committed at the end of the transaction yet... and I'm not sure how to restructure my test case to get this to work.
Here's my over-simplified test case to demonstrate my problem:-
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {
"classpath:applicationContext-test.xml"
})
@TransactionConfiguration(transactionManager = "transactionManager")
@Transactional
public class SomeTest {
@Autowired
protected DataSource dataSource;
@Autowired
private SessionFactory sessionFactory;
@Test
public void testThis() throws Exception {
Session session = sessionFactory.getCurrentSession();
assertEquals("initial overlayType count", 4, session.createQuery("from OverlayType").list().size());
//-----------
// Imagine this block is an API call, ex: someService.save("AAA");
// But for the sake of simplicity, I do it this way
OverlayType overlayType = new OverlayType();
overlayType.setName("AAA");
session.save(overlayType);
//-----------
// flush has no effect here
session.flush();
assertEquals("new overlayType count", 5, session.createQuery("from OverlayType").list().size());
// pull the data from database using dbunit
IDatabaseConnection connection = new DatabaseConnection(dataSource.getConnection());
connection.getConfig().setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY, new MySqlDataTypeFactory());
QueryDataSet partialDataSet = new QueryDataSet(connection);
partialDataSet.addTable("resultSet", "select * from overlayType");
ITable actualTable = partialDataSet.getTable("resultSet");
// FAIL: Actual row count is 4 instead of 5
assertEquals("dbunit's overlayType count", 5, actualTable.getRowCount());
DataSourceUtils.releaseConnection(connection.getConnection(), dataSource);
}
}
My whole idea in using DbUnit is to:-
- Call
someService.save(...)
that saves data into several tables. - Use DbUnit to get expected table from XML.
- Use DbUnit to get actual table from database.
- Do
Assertion.assertEquals(expectedTable, actualTable);
.
But, at this point, I'm not able to get DbUnit to see the changes made by Hibernate within the transaction.
How should I change to get DbUnit to work nicely with Hibernate transaction?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我从未使用过 DbUnit,但似乎 < code>TransactionAwareDataSourceProxy 就可以了。基本上,您需要使用此代理包装原始数据源并使用它,以便此代码:
实际上通过代理并使用与 Hibernate 相同的事务和连接。
我发现 事务感知数据源(使用 dbunit & 在春天休眠) 博客文章解释了这一点。
另一种方法是完全跳过事务测试并手动清理数据库。查看我的被认为有害的事务测试文章。
I have never worked with DbUnit, but it seems like
TransactionAwareDataSourceProxy
will do the trick. Basically you need to wrap your original data source with this proxy and use it instead, so that this code:actually goes through the proxy and uses the same transaction and connection as Hibernate.
I found Transaction aware datasource (use dbunit & hibernate in spring) blog post explaining this.
Another approach would be to skip transactional tests altogether and cleanup the database instead manually. Check out my transactional tests considered harmful artcle.
看起来该测试用例需要两个事务:一个用于将数据放入数据库,第二个用于检索数据。
我要做的是:
初始覆盖类型计数将为 0,保存会话后,它应该为 1。
Looks like that test case needs two transactions: one for putting data into the database, and a second one to retrieve it.
What I would do is:
The initial overlaytype count would be 0, and after the session is saved, it should be 1.