[英]How to reset database after each test when using jeeunit?
I was wondering if there is anyone who have had success in clearing out database contents before each test when using jeeunit (or any other java ee application test framework)? 我想知道是否有人在使用jeeunit(或任何其他Java ee应用程序测试框架)进行每次测试之前成功清除数据库内容?
I've tried using the @Transactional annotation as described in jeeunit documentation where it will rollback all transactions done. 我已尝试使用jeeunit文档中所述的@Transactional批注,在该批注中将回滚所有已完成的事务。 However there are cases (session is flushed) where the changes done is persisted between tests, causing test pollution.
但是,在某些情况下(会话被清除),所做的更改会在测试之间持续存在,从而导致测试污染。
Any help/guidance is appreciated! 任何帮助/指导表示赞赏!
Since you have mentioned 'any other java ee application test framework' I would recommend looking into Arquillian Persistence Extension for unit testing a Java EE application. 既然您提到了“任何其他Java ee应用程序测试框架”,我建议您研究Arquillian Persistence Extension,以对Java EE应用程序进行单元测试。 You can find a tutorial on how to use it here: http://www.softwarepassion.com/java-ee-6-testing-with-arquillian-persistence-extension/
您可以在此处找到有关如何使用它的教程: http : //www.softwarepassion.com/java-ee-6-testing-with-arquillian-persistence-extension/
More info about arquillian itself here 有关Arquillian本身的更多信息,请点击此处
Spring framework 's AbstractTransactionalJUnit4SpringContextTests is another good beginning. Spring框架的AbstractTransactionalJUnit4SpringContextTests是另一个好的开始。
And you can do it yourself by set autoCommit(false) before execute on your connection, and rollback after your code run. 您可以通过在连接执行之前设置autoCommit(false)来自己完成操作,并在代码运行后回滚。
我的解决方案是将jeeunit与h2 db一起使用,并在每次测试之前通过本机SQL强制删除数据库中的所有行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.