简体   繁体   English

JUnit测试后清理数据库。

[英]Clean up database after JUnit test.

I have a unit test that inserts rows into the database, and I'd like DBUnit to automatically clean up those rows afterwards (DBUnit does not initially insert those rows - that's purely the responsibility of the code being tested). 我有一个将行插入数据库的单元测试,并且我希望DBUnit之后自动清除这些行(DBUnit最初不插入那些行-纯粹是被测试的代码的职责)。

Thanks. 谢谢。

使您的INSERT / UPDATE / DELETE查询具有事务性,并在测试完成后回滚它们。

If you use JUnit4, then you can declare a function with @AfterClass annotation: it will execute after all tests and you can remove all the rows you added in your tables. 如果使用JUnit4,则可以声明带有@AfterClass批注的函数:它将在所有测试之后执行,并且可以删除在表中添加的所有行。

In JUnit3, you don't have any equivalent, but you can override the tearDown() method, which will be executed after each test (equivalent of @After annotation in JUnit4). 在JUnit3中,您没有任何等效项,但是您可以重写tearDown()方法,该方法将在每次测试后执行(等效于JUnit4中的@After注释)。

If you allocate external resources(file/DB) in a Before method you need to release them after the test runs. 如果您使用Before方法分配外部资源(文件/数据库),则需要在测试运行后释放它们。 Annotating a public void method with @After causes that method to be run after the Test method. 用@After注释公共无效方法会导致该方法在Test方法之后运行。 All @After methods are guaranteed to run even if a Before or Test method throws an exception. 即使@Before或Test方法引发异常,也保证所有@After方法都可以运行。 The @After methods declared in superclasses will be run after those of the current class. 在超类中声明的@After方法将在当前类的方法之后运行。

Example shows file as resource but you can use it for your DB cleanup. 示例将文件显示为资源,但您可以将其用于数据库清理。

 public class Example {
    File output;
    @Before public void createOutputFile() {
          output= new File(...);
    }
    @Test public void something() {
          ...
    }
    @After public void deleteOutputFile() {
          output.delete();
    }
 }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 升级到Spring 3和junit 11-运行测试后上下文变为空。 没有可用于第二次测试的自动装配的Bean - Upgraded to Spring 3 and junit 11 - Context becoming null after running a test. No autowired bean available for the second test 设置JUnit测试数据库 - Set up JUnit test database 在测试过程中模拟鼠标单击。 JUnit,JavaFX - Simulate a mouse click during test. JUnit, JavaFX 在 Junit 测试中使用 assertEquals。 我该如何纠正? - Using assertEquals in Junit test. How can I correct it? 如果测试不在测试目录中但在主目录中并且类名没有。* Test。*之类的关键字,如何在Maven中运行JUnit Test - How to run JUnit Test in maven if the test are not in test dir but in main dir and the classname are not having keywords like .*Test.* 在单元测试中使用HttpClient后如何正确清理 - How to properly clean up after using an HttpClient in a unit test 我有两种方法需要使用JUnit测试进行测试。 这是代码 - I have two methods that need to be tested using the JUnit test. Here are the codes 什么时候应该在代码中使用assert而不是编写junit测试。 他们是一样的吗? - When should I use assert in code, instead of writing a junit test. Are they the same? JUnit 测试。 使用ModelMapper库将entity转换为DTO时的问题 - JUnit test. The problem when converting entity to DTO by using ModelMapper library 运行JUnit测试后进行的数据库更改不可见 - database changes made after a running a JUnit test are not visible
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM