Mateusz Chromiński Mateusz Chromiński - 1 year ago 196
Java Question

Cleanup after all junit tests

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 tests. I don't want to keep some magic static number referring the number of all test methods, which I should maintain all the time.

The most appreciated way is to add some listener which would be invoked after all the tests. Is there any interface for it already in JUnit4?

edit: this has nothing to do with @BeforeClass and @AfterClass, cause I have to know if method annotated with @AfterClass is invoked for the last time.

Answer Source

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;

public class RunTestSuite extends TestCase {
    public static void doYourOneTimeSetup() {

    public static void doYourOneTimeTeardown() {

Edit: I am quite positive (unless I misunderstand you 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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download