简体   繁体   English

使用jeeunit时,每次测试后如何重置数据库?

[英]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本身的更多信息,请点击此处

DBUnit is designed to assist with this exact problem - their website is here DBUnit旨在解决这个确切的问题-他们的网站在这里

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.

相关问题 每个测试单元 function 后如何重置数据库? - How to reset the database after each test unit function? 使用 Spring Boot 和 Liquibase 时,如何在每次集成测试后清理数据库表? - How to clean database tables after each integration test when using Spring Boot and Liquibase? 如何重置 spring 项目中的每个测试的数据库? - How can I reset database each test in spring project? JUnit-@DirtyContext在每种测试方法后未重置neo4j数据库 - JUnit - @DirtyContext not reset neo4j database after each test method 如何让 Spring Cloud Contract 在每次测试之前或之后重置 WireMock - How to make Spring Cloud Contract reset WireMock before or after each test 如何在每个 if 语句后重置数组? - How to reset an array after each if statement? 是否可以在不使用 Spring 框架的情况下在每个 JUnit 测试用例之后回滚数据库? - Is it possible to rollback the database after each JUnit test case without using Spring framework? Spring AbstractTransactionalDataSourceSpringContextTests-防止在每次Junit测试之后数据库回滚 - Spring AbstractTransactionalDataSourceSpringContextTests - Preventing database rollback after each Junit test 每次动态测试后如何执行代码? - How execute code after each dynamic test? 使用 GRADLEW BUILD 运行时如何在每个测试类之间删除 H2 数据库? - How to drop H2 database between each test class when running with GRADLEW BUILD?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM