Cleanup after all junit tests

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

提交回复
热议问题