简体   繁体   中英

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)?

I've tried using the @Transactional annotation as described in jeeunit documentation where it will rollback all transactions done. 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. You can find a tutorial on how to use it here: http://www.softwarepassion.com/java-ee-6-testing-with-arquillian-persistence-extension/

More info about arquillian itself here

DBUnit is designed to assist with this exact problem - their website is here

Spring framework 's AbstractTransactionalJUnit4SpringContextTests is another good beginning.

And you can do it yourself by set autoCommit(false) before execute on your connection, and rollback after your code run.

我的解决方案是将jeeunit与h2 db一起使用,并在每次测试之前通过本机SQL强制删除数据库中的所有行。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM