Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 641
Mateusz Chromiński Avatar asked Mar 28 '12 07:03

Mateusz Chromiński


People also ask

Which method runs after end of each test case?

@After annotation is used on a method containing java code to run after each test case. These methods will run even if any exceptions are thrown in the test case or in the case of assertion failures.

Which annotation causes that method to run once after all tests have finished?

Explanation. Annotating a public void method with @After causes that method to be run after each Test method.

Which annotation is used to signal that the annotated method should be executed after each test in the current test class?

@AfterAll is used to signal that the annotated method should be executed after all tests in the current test class. @AfterEach is used to signal that the annotated method should be executed after each @Test method in the current test class.


2 Answers

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.

like image 56
HellishHeat Avatar answered Sep 27 '22 20:09

HellishHeat


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> 
like image 35
Juraj Michalak Avatar answered Sep 27 '22 20:09

Juraj Michalak