Search code examples
spring-bootjunitintegration-testingspring-boot-testjunit-jupiter

Junit5- jupiter all tests suite @BeforeAll @AfterAll not working


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


Solution

  • 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).