Getting DbUnit to Work with Hibernate Transaction

半腔热情 提交于 2019-12-04 07:50:59

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:

new DatabaseConnection(dataSource.getConnection())

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.

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:

  • Use a memory database so the data is cleaned when the unit test ends.
  • Remove the transactional annotations and use the beginTransaction and commit methods of the session directly.

The initial overlaytype count would be 0, and after the session is saved, it should be 1.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!