6

The before and after methods not working in JUnitPlatform.

The code is below.

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.platform.runner.JUnitPlatform;
import org.junit.platform.suite.api.SelectClasses;
import org.junit.runner.RunWith;

@RunWith(JUnitPlatform.class)
@SelectClasses({
        MyControllerTest.class
})
public class AdminAppTest {

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

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

I just want to running before and after methods.

Thank you

Mehmet Bektaş
  • 173
  • 1
  • 2
  • 15

3 Answers3

7

Just came across this after running into the same problem, @AfterAll and also @AfterEach annotated methods weren't called. In my case the problem was that an incorrect import for JUnit4 sneaked into my test class, I was importing org.junit.Test instead of org.junit.jupiter.api.Test. Once I fixed this my annotated methods were happily called again.

krschn
  • 153
  • 2
  • 6
3

I may cite the JUnit 5 migration tipps:

@Before and @After no longer exist; use @BeforeEach and @AfterEach instead.

@BeforeClass and @AfterClass no longer exist; use @BeforeAll and @AfterAll instead.

But these annotations should be used in your test class. The methods in your suite class will not be invoked this way. And you should be aware of the fact that suites and JUnit 5 are still work in progress (see Issue 744).

Jens Dibbern
  • 1,434
  • 2
  • 13
  • 20
1

You may want to check that org.junit.jupiter:junit-jupiter-engine is in your classpath.

However, recent versions of build tools (Maven with surefire plugin, Gradle, Eclipse, IntelliJ, etc.) support junit-platform. Therefore you do not need to use JUnit4 with a backward-compatible runner.

Usually you can be in the following cases:

  • creating new project, in which case you can start directly using only JUnit-Jupiter (and without having JUnit4 in your classpath)

  • migrating a project from JUnit4 to JUnit5. Here you will want to have two engines: JUnit-Vintage, which covers retrocompatibility for existing tests using the JUnit4 API, and JUnit-Jupiter who offers more flexibility, including the composition of extensions (having Spring, Mockito and parameterized tests features at the same time for example)

Using a JUnit4 runner to run JUnit-Jupiter tests is really a corner case, when you are constrained by an environment (build tool and/or IDE) that you cannot change.

For more details, sample projects are made available by the JUnit team:

Hope this helps !

Loïc Le Doyen
  • 975
  • 7
  • 16
  • Isn't org.junit.jupiter:junit-jupiter-api also requried on the classpath? – Woodchuck Feb 05 '22 at 23:37
  • 1
    Sure it is, but it is a transitive dependency of junit-jupiter-engine. So if you are using a conventionnal build tool such as Maven or Gradle, you do not have to worry about it. Moreover, nowadays we use the `org.junit.jupiter:junit-jupiter` dependency, which embed all jupiter-related dependencies and is more of a starter as you may find in the Spring-Boot galaxy. In most cases, there is no need to have explicit dependency to jupiter-api or jupiter-engine anymore. – Loïc Le Doyen Feb 07 '22 at 08:12
  • Got it. Thanks! – Woodchuck Feb 07 '22 at 13:43