Search code examples
junitrefactoringtestngtest-suite

JUnit equivalents for TestNG's @BeforeSuite, @BeforeTest


I'm refactoring some test classes from TestNG to JUnit 4. During the process, I've stumbled upon the following annotations:

@BeforeTest
@AfterTest 

According to the manual:

The annotated method will be run before/after any test method belonging to the classes inside the tag is run.

What would be the equivalent annotations in JUnit?


Solution

  • This is the original answer, but I think it is wrong. See below for a better one

    The equivalent would be the annotations

    @Before
    

    and

    @After
    

    see also http://junit.sourceforge.net/javadoc/org/junit/Before.html

    This is a better answer, after I learned about the difference between Before/AfterMethod and Before/AfterTest in TestNG

    If I got it right, with Before/AfterTest you can run a method before or after a list of tests, that you specify inside the annotation or a separate document.

    There is no out of the box feature like this in JUnit.

    Probably the best you can do, is put what ever you want to do in a JUnit Rule. See also http://schauderhaft.de/2011/07/24/rules-in-junit-4-9-beta-3/

    Then you can use that Rule in any test that needs it.