我如何 运行 使用 Maven Failsafe 插件进行 JUnit 5 集成测试?

How do I run JUnit 5 integration tests with the Maven Failsafe plugin?

当我 运行 命令 mvn clean failsafe:integration-test 时,Maven Failsafe 插件找不到我的 JUnit 5 集成测试,尽管它可以找到文件。

我有 junit-jupiter-apijunit-jupiter-engine 作为测试依赖项:

<properties>
    <junit.jupiter.version>5.0.1</junit.jupiter.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-api</artifactId>
        <version>${junit.jupiter.version}</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-engine</artifactId>
        <version>${junit.jupiter.version}</version>
        <scope>test</scope>
    </dependency>
</dependencies>

我的集成测试命名正确(遵循 **/*IT.java**/IT*.java**/*ITCase.java,Failsafe 默认包含而 Surefire 默认排除)。

有什么方法可以将 JUnit 5 测试与 Failsafe 一起使用?

编辑: 这个答案在 maven-failsafe-plugin:2.22.0 之前是正确的。请参阅 以获得理想的最新解决方案。


maven-failsafe-plugin 当前 doesn't support JUnit 5,开箱即用。

但是,,您可以 运行 使用 maven-failsafe-plugin 进行 JUnit 5 测试,方法是指定 org.junit.platform:junit-platform-surefire-provider:1.0.1 与早期版本 [=16] 的依赖关系=].

由于 OutOfMemory error.

它不适用于当前版本的 2.20 故障保护(与 surefire 出现错误的方式相同)

查看下面的插件配置示例:

<properties>
    <junit.platform.version>1.0.1</junit.platform.version>
</properties>

<plugin>
    <artifactId>maven-failsafe-plugin</artifactId>
    <version>2.19.1</version>
    <dependencies>
        <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-surefire-provider</artifactId>
            <version>${junit.platform.version}</version>
        </dependency>
    </dependencies>
</plugin>

您可以找到 a full example of this working (and a failing one) on GitHub。要测试它是否有效,您可以 运行 mvn clean failsafe:integration-test.

请注意,从 the JUnit 5 documentation 开始:junit-platform-surefire-provider 不应再使用:

Due to the release of Surefire 2.22.0, the junit-platform-surefire-provider from the JUnit team has been deprecated and will be discontinued in a subsequent release of the JUnit Platform.

此外,您还可以阅读 maven-surefire-plugin documentation :

Using JUnit 5 Platform

To get started with JUnit Platform, you need to add at least a single TestEngine implementation to your project. For example, if you want to write tests with Jupiter, add the test artifact junit-jupiter-engine to the dependencies in POM

因此您必须指定此 test 依赖项:

<properties>
    <junit-jupiter.version>5.2.0</junit-jupiter.version>
</properties> 

<dependencies>
     [...]
     <dependency>
         <groupId>org.junit.jupiter</groupId>
         <artifactId>junit-jupiter-engine</artifactId>
         <version>${junit-jupiter.version}</version>
         <scope>test</scope>
     </dependency>
     [...] 
</dependencies>

并且 maven-failsafe-plugin 声明可以像这样简单:

<build>
    <plugins>           
        <plugin>
            <artifactId>maven-failsafe-plugin</artifactId>
            <version>2.22.0</version>
        </plugin>
    </plugins>
</build>