Spring boot Junit5-jupiter所有测试套件@BeforeAll@AfterAll不工作

Spring boot Junit5-jupiter所有测试套件@BeforeAll@AfterAll不工作,spring-boot,junit,integration-testing,spring-boot-test,junit-jupiter,Spring Boot,Junit,Integration Testing,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;

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");
    }
}
我只想运行前后方法

谢谢

我可以引用JUnit 5:

在之前的@和在之后的@不再存在;改为在每次之前使用
@和每次
之后使用
@

@BeforeClass
@AfterClass
不再存在;改为使用前后的
@BeforeAll
@aftereall


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

您可能需要检查org.JUnit.jupiter:JUnit-jupiter引擎是否在您的类路径中

但是,最新版本的构建工具(带有surefire插件的Maven、Gradle、Eclipse、IntelliJ等)支持junit平台。因此,您不需要将JUnit4与向后兼容的运行程序一起使用

通常,在以下情况下,您可以:

  • 创建新项目,在这种情况下,您可以直接使用JUnitJupiter(并且在类路径中没有JUnit4)

  • 将项目从JUnit4迁移到JUnit5。在这里,您需要两个引擎:JUnit Vintage,它涵盖了使用JUnit4 API的现有测试的可追溯性;JUnit Jupiter提供了更大的灵活性,包括扩展的组合(同时具有Spring、Mockito和参数化测试功能)

当您受到无法更改的环境(构建工具和/或IDE)的约束时,使用JUnit4运行程序运行JUnit Jupiter测试实际上是一种极端情况

有关更多详细信息,JUnit团队提供了示例项目:

希望这有帮助