Cleanup after all junit tests

前端 未结 5 1078
小鲜肉
小鲜肉 2020-12-02 22:38

In my project I have to do some repository setup before all tests. This is done using some tricky static rules. However I\'ve got no clue how to do clean up after all the te

5条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-02 23:12

    I recommend to use org.junit.runner.notification.RunListener, example:

    public class TestListener extends RunListener {
      @Override
      public void testRunStarted(Description description) throws Exception {
         // Called before any tests have been run.
      }
      @Override
      public void testRunFinished(Result result) throws Exception {
         // Called when all tests have finished
      }
    }
    

    Read more directly in JUnit java doc. You can use that even with Maven's surefire (unit tests) plugin or failsafe plugin (integration tests) by adding following code into plugin configuration:

    
      
        listener
        com.innovatrics.afismq.it.TestListener
      
    
    

提交回复
热议问题