首页 > 解决方案 > Junit5-木星所有测试套件@BeforeAll @AfterAll 不工作

问题描述

之前和之后的方法在 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");
    }
}

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

谢谢

标签: spring-bootjunitintegration-testingspring-boot-testjunit-jupiter

解决方案


我可以引用 JUnit 5迁移提示

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

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

但是这些注释应该在你的测试类中使用。您的套件类中的方法不会以这种方式调用。您应该知道套件和 JUnit 5 仍在进行中的事实(请参阅问题 744)。


推荐阅读