Junit5-jupiter 所有测试套件 @BeforeAll @AfterAll 不起作用

Meh*_*taş 6 junit integration-testing spring-boot spring-boot-test junit-jupiter

before 和 after 方法在 JUnitPlatform 中不起作用。

代码如下。

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");
    }
}
Run Code Online (Sandbox Code Playgroud)

我只想在方法之前和之后运行。

谢谢

krs*_*chn 7

在遇到同样的问题后刚刚遇到这个问题,@AfterAll 和 @AfterEach 带注释的方法都没有被调用。就我而言,问题是 JUnit4 的错误导入潜入了我的测试类,我导入的是 org.junit.Test 而不是 org.junit.jupiter.api.Test。一旦我解决了这个问题,我的带注释的方法就很高兴地再次被调用。


小智 3

我可以引用 JUnit 5迁移技巧

@Before并且@After不再存在;使用@BeforeEachand@AfterEach代替。

@BeforeClass并且@AfterClass不再存在;使用@BeforeAlland@AfterAll代替。

但这些注释应该在您的测试类中使用。套件类中的方法不会以这种方式调用。您应该意识到套件和 JUnit 5 仍在开发中(请参阅问题 744)。