Before and After Suite execution hook in jUnit 4.x
Asked Answered
M

11

85

I'm trying to preform setup and teardown for a set of integration tests, using jUnit 4.4 to execute the tests. The teardown needs to be run reliably. I'm having other problems with TestNG, so I'm looking to port back to jUnit. What hooks are available for execution before any tests are run and after all tests have completed?

Note: we're using maven 2 for our build. I've tried using maven's pre- & post-integration-test phases, but, if a test fails, maven stops and doesn't run post-integration-test, which is no help.

Marchak answered 17/9, 2008 at 13:7 Comment(2)
For integration tests you should use the maven-failsafe-plugin instead of surefire. This will not skip post-integration-test if a test fails. See also this wiki page.Ophthalmology
can you share you final implementation please ?Antonietta
B
118

Yes, it is possible to reliably run set up and tear down methods before and after any tests in a test suite. Let me demonstrate in code:

package com.test;

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({Test1.class, Test2.class})
public class TestSuite {

    @BeforeClass
    public static void setUp() {
        System.out.println("setting up");
    }

    @AfterClass
    public static void tearDown() {
        System.out.println("tearing down");
    }

}

So your Test1 class would look something like:

package com.test;

import org.junit.Test;


public class Test1 {
    @Test
    public void test1() {
        System.out.println("test1");
    }

}

...and you can imagine that Test2 looks similar. If you ran TestSuite, you would get:

setting up
test1
test2
tearing down

So you can see that the set up/tear down only run before and after all tests, respectively.

The catch: this only works if you're running the test suite, and not running Test1 and Test2 as individual JUnit tests. You mentioned you're using maven, and the maven surefire plugin likes to run tests individually, and not part of a suite. In this case, I would recommend creating a superclass that each test class extends. The superclass then contains the annotated @BeforeClass and @AfterClass methods. Although not quite as clean as the above method, I think it will work for you.

As for the problem with failed tests, you can set maven.test.error.ignore so that the build continues on failed tests. This is not recommended as a continuing practice, but it should get you functioning until all of your tests pass. For more detail, see the maven surefire documentation.

Bobbiebobbin answered 7/10, 2008 at 2:51 Comment(6)
This worked perfectly for me once I went into the maven-surefire-plugin and created an includes listing that pointed to the suite I wanted to run.Linnet
As of JUnit 4.8.2, this doesn't play well with parameterized tests. The Suite's @BeforeClass methods will be run after the @Parameterized.Parameters method of the test, preventing any dependence on the Suite's setup.Hoofer
In response to myself, when using @Theories, the call to the @DataPoints method is call after the @BeforeClass of the Suite.Hoofer
Sorry for the necro - but adding BeforeClass / AfterClass to a super class doesn't work as expected - they are still called after each test class completes. This is for posterity.Wane
Is this still a valid approach? How do you get avoid the need to enumerate the list of test classes in the SuiteClasses annotation?Grobe
@SubuSankaraSubramanian see this answer: https://mcmap.net/q/162857/-detect-if-junit-test-class-is-running-in-a-suite-or-alonePuttier
M
37

A colleague of mine suggested the following: you can use a custom RunListener and implement the testRunFinished() method: http://junit.sourceforge.net/javadoc/org/junit/runner/notification/RunListener.html#testRunFinished(org.junit.runner.Result)

To register the RunListener just configure the surefire plugin as follows: http://maven.apache.org/surefire/maven-surefire-plugin/examples/junit.html section "Using custom listeners and reporters"

This configuration should also be picked by the failsafe plugin. This solution is great because you don't have to specify Suites, lookup test classes or any of this stuff - it lets Maven to do its magic, waiting for all tests to finish.

Madelyn answered 7/2, 2013 at 17:29 Comment(1)
+1 That's the first usable solution I've seen without the cumbersome maintenance of a Suites class!Ancon
N
13

You can use the @ClassRule annotation in JUnit 4.9+ as I described in an answer another question.

Nolie answered 3/10, 2011 at 18:15 Comment(0)
B
5

Using annotations, you can do something like this:

import org.junit.*;
import static org.junit.Assert.*;
import java.util.*;

class SomethingUnitTest {
    @BeforeClass
    public static void runBeforeClass()
    {

    }

    @AfterClass
    public static void runAfterClass()
    {  

    }

    @Before  
    public void setUp()
    {

    }

    @After
    public void tearDown()
    {

    }

    @Test
    public void testSomethingOrOther()
    {

    }

}
Benavidez answered 17/9, 2008 at 13:32 Comment(2)
The setup & teardown need to be executed once per run. This would only help if all the tests are in one class.Marchak
This only sets up the individual test suite, not the entire testing stepAraliaceous
P
3

Here, we

  • upgraded to JUnit 4.5,
  • wrote annotations to tag each test class or method which needed a working service,
  • wrote handlers for each annotation which contained static methods to implement the setup and teardown of the service,
  • extended the usual Runner to locate the annotations on tests, adding the static handler methods into the test execution chain at the appropriate points.
Pyramid answered 1/4, 2009 at 18:13 Comment(0)
M
2

As for "Note: we're using maven 2 for our build. I've tried using maven's pre- & post-integration-test phases, but, if a test fails, maven stops and doesn't run post-integration-test, which is no help."

you can try the failsafe-plugin instead, I think it has the facility to ensure cleanup occurs regardless of setup or intermediate stage status

Marketa answered 7/12, 2010 at 14:54 Comment(1)
Yes, the failsafe plugin will allow you to specify specific setup and teardown. Although I don't think failsafe existed at the time this question was posted.Rimmer
C
2

Provided that all your tests may extend a "technical" class and are in the same package, you can do a little trick :

public class AbstractTest {
  private static int nbTests = listClassesIn(<package>).size();
  private static int curTest = 0;

  @BeforeClass
  public static void incCurTest() { curTest++; }

  @AfterClass
  public static void closeTestSuite() {
      if (curTest == nbTests) { /*cleaning*/ }             
  }
}

public class Test1 extends AbstractTest {
   @Test
   public void check() {}
}
public class Test2 extends AbstractTest {
   @Test
   public void check() {}
}

Be aware that this solution has a lot of drawbacks :

  • must execute all tests of the package
  • must subclass a "techincal" class
  • you can not use @BeforeClass and @AfterClass inside subclasses
  • if you execute only one test in the package, cleaning is not done
  • ...

For information: listClassesIn() => How do you find all subclasses of a given class in Java?

Chloroprene answered 27/9, 2011 at 15:51 Comment(1)
this is not true as far as my own tests show. I have a super class that starts an embedded glassfish on beforeclass and shuts it down on after class. I have then 2 classes that extend from that super class. The beforeclass gets executed before running the tests defined in each class.Madeira
C
0

As far as I know there is no mechanism for doing this in JUnit, however you could try subclassing Suite and overriding the run() method with a version that does provide hooks.

Culosio answered 17/9, 2008 at 13:28 Comment(0)
B
0

The only way I think then to get the functionality you want would be to do something like

import junit.framework.Test;  
import junit.framework.TestResult;  
import junit.framework.TestSuite;  

public class AllTests {  
    public static Test suite() {  
        TestSuite suite = new TestSuite("TestEverything");  
        //$JUnit-BEGIN$  
        suite.addTestSuite(TestOne.class);  
        suite.addTestSuite(TestTwo.class);  
        suite.addTestSuite(TestThree.class);  
        //$JUnit-END$  
     }  

     public static void main(String[] args)  
     {  
        AllTests test = new AllTests();  
        Test testCase = test.suite();  
        TestResult result = new TestResult();  
        setUp();  
        testCase.run(result);  
        tearDown();  
     }  
     public void setUp() {}  
     public void tearDown() {}  
} 

I use something like this in eclipse, so I'm not sure how portable it is outside of that environment

Benavidez answered 17/9, 2008 at 13:50 Comment(1)
This is an example for JUnit3, and the OP asked for JUnit4, but just in case some JUnit3 users find this question... For JUnit3, it would be better to get rid of the main() method and have the suite() method wrap the TestSuite in a subclass of junit.extensions.TestSetup. You still have the same caveats as Julie's example about running the individual test classes in your IDE.Uteutensil
C
0

Since maven-surefire-plugin does not run Suite class first but treats suite and test classes same, so we can configure plugin as below to enable only suite classes and disable all the tests. Suite will run all the tests.

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.5</version>
            <configuration>
                <includes>
                    <include>**/*Suite.java</include>
                </includes>
                <excludes>
                    <exclude>**/*Test.java</exclude>
                    <exclude>**/*Tests.java</exclude>
                </excludes>
            </configuration>
        </plugin>
Caesarean answered 23/6, 2010 at 17:39 Comment(0)
Z
0

If you don't want to create a suite and have to list all your test classes you can use reflection to find the number of test classes dynamically and count down in a base class @AfterClass to do the tearDown only once:

public class BaseTestClass
{
    private static int testClassToRun = 0;

    // Counting the classes to run so that we can do the tear down only once
    static {
        try {
            Field field = ClassLoader.class.getDeclaredField("classes");
            field.setAccessible(true);

            @SuppressWarnings({ "unchecked", "rawtypes" })
            Vector<Class> classes = (Vector<Class>) field.get(BlockJUnit4ClassRunner.class.getClassLoader());
            for (Class<?> clazz : classes) {
                if (clazz.getName().endsWith("Test")) {
                    testClassToRun++;
                }
            }
        } catch (Exception ignore) {
        }
    }

    // Setup that needs to be done only once
    static {
        // one time set up
    }

    @AfterClass
    public static void baseTearDown() throws Exception
    {
        if (--testClassToRun == 0) {
            // one time clean up
        }
    }
}

If you prefer to use @BeforeClass instead of the static blocks, you can also use a boolean flag to do the reflection count and test setup only once at the first call. Hope this helps someone, it took me an afternoon to figure out a better way than enumerating all classes in a suite.

Now all you need to do is extend this class for all your test classes. We already had a base class to provide some common stuff for all our tests so this was the best solution for us.

Inspiration comes from this SO answer https://mcmap.net/q/162859/-cleaning-up-after-all-junit-tests-without-explicit-test-suite-classes-declaration

If you don't want to extend this class everywhere, this last SO answer might do what you want.

Zonda answered 25/10, 2016 at 22:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.