Cleanup after all junit tests

前端 未结 5 991
小鲜肉
小鲜肉 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:00

    Just encountered the same problem.
    My solution:

    • For a global set up: use (lazy) singleton to access something global that requires instantiation before the tests. The first test that accesses this singleton will trigger the global set up process.
    • For a global tear down: use a Java shutdown hook:
      Runtime.getRuntime().addShutdownHook(new Thread(() -> do_your_global_cleanup())));
    0 讨论(0)
  • 2020-12-02 23:03

    You can always write your custom TestRunner. However, before you do that you need to evaluate the need for the same. It is better to use @BeforeClass and @AfterClass. Another example I can point to is, the fashion in which hibernate allows users to do unit testing using 'import.sql'.

    0 讨论(0)
  • 2020-12-02 23:07

    No need to use suite, just add @BeforeClass, and @AfterClass as static

    public class Tests {
    
        @BeforeClass
        public static void doYourOneTimeSetup()
        {
            ...
        }
    
        @AfterClass
        public static void doYourOneTimeTeardown() {
            ...
        }    
    
        @Test
        public void testYourTestcase()
        {
            ...
        }
    }
    
    0 讨论(0)
  • 2020-12-02 23:08

    I'm using JUnit 4.9. Will this help?:

    import junit.framework.TestCase;
    
    import org.junit.AfterClass;
    import org.junit.BeforeClass;
    import org.junit.runner.RunWith;
    import org.junit.runners.Suite;
    import org.junit.runners.Suite.SuiteClasses;
    
    @RunWith(Suite.class)
    @SuiteClasses({First.class,Second.class,Third.class})
    public class RunTestSuite extends TestCase {
        @BeforeClass
        public static void doYourOneTimeSetup() {
            ...
        }
    
        @AfterClass
        public static void doYourOneTimeTeardown() {
            ...
        }    
    }
    

    Edit: I am quite positive (unless I misunderstand your question) that my solution is what you are looking for. i.e. one teardown method after all your tests have ran. No listener required, JUnit has this facility. Thanks.

    0 讨论(0)
  • 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:

    <properties>
      <property>
        <name>listener</name>
        <value>com.innovatrics.afismq.it.TestListener</value>
      </property>
    </properties>
    
    0 讨论(0)
提交回复
热议问题