Surefire无法识别Junit 5测试。

157

我使用JUnit 5编写了一个简单的测试方法:

public class SimlpeTest {
    @Test
    @DisplayName("Some description")
    void methodName() {
        // Testing logic for subject under test
    }
}

但是当我运行 mvn test 命令时,出现了以下错误:

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running SimlpeTest
Tests run: 0, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.001 sec

Results :

Tests run: 0, Failures: 0, Errors: 0, Skipped: 0

一些问题,surefire 没能识别出测试类。我的pom.xml文件如下:

<properties>
    <java.version>1.8</java.version>
    <junit.version>5.0.0-SNAPSHOT</junit.version>
</properties>

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

<repositories>
    <repository>
        <id>snapshots-repo</id>
        <url>https://oss.sonatype.org/content/repositories/snapshots</url>
        <releases>
            <enabled>false</enabled>
        </releases>
        <snapshots>
            <updatePolicy>always</updatePolicy>
            <enabled>true</enabled>
        </snapshots>
    </repository>
</repositories>

<build>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>${java.version}</source>
                <target>${java.version}</target>
            </configuration>
        </plugin>
    </plugins>
</build>

你有任何想法可以让这个工作起来吗?


1
现在你必须使用一个特殊的surefire-plugin实现。请查看junit团队的示例(此处) - Hendrik Jander
1
这个问题基于一个过时的版本,但是这个答案是基于当前版本(截至2016年12月)的。 - Nicolai Parlog
@Nicolai 感谢您更新答案。如果您有时间,我会很感激您对问题的编辑。 - Ali Dehghani
1
这个错误不再以那种方式出现了。缺乏测试执行的最可能情况已被这个问题涵盖。 - Nicolai Parlog
确保测试文件在正确的位置。默认情况下,测试文件应该是 src 文件夹下 main 的同级目录。 - Joe Bowbeer
请查看下面的答案(https://dev59.com/8loU5IYBdhLWcg3w551Q#51838295)以获取当前版本(2.22.x)的surefire-plugin。 将<artifactId> junit-jupiter-engine </ artifactId>或<artifactId> junit-jupiter </ artifactId>添加为依赖项(不在插件中)可能有所帮助。 - MichaelCkr
20个回答

138

截至今天,maven-surefire-plugin还没有完全支持JUnit 5 (相关链接1)。关于添加此支持的问题已经有了一个开放的问题SUREFIRE-1206

因此,您需要使用自定义提供程序。JUnit团队已经开发了一个提供程序;从用户指南中,您需要添加 junit-platform-surefire-provider提供程序和新API的TestEngine实现:

<build>
  <plugins>        
    <plugin>
      <artifactId>maven-surefire-plugin</artifactId>
      <!-- latest version (2.20.1) does not work well with JUnit5 -->
      <version>2.19.1</version>
      <dependencies>
        <dependency>
          <groupId>org.junit.platform</groupId>
          <artifactId>junit-platform-surefire-provider</artifactId>
          <version>1.0.3</version>
        </dependency>
        <dependency>
          <groupId>org.junit.jupiter</groupId>
          <artifactId>junit-jupiter-engine</artifactId>
          <version>5.0.3</version>
        </dependency>
      </dependencies>
    </plugin>
  </plugins>
</build>

此外,请确保使用test范围声明junit-jupiter-api依赖项:
<dependencies>
  <dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-api</artifactId>
    <version>5.0.3</version>
    <scope>test</scope>
  </dependency>
</dependencies>

25
值得注意的是,目前的surefire 2.20.1与junit 5 provider不兼容,因此尽管已经过去了一年,但示例中使用的2.19.1仍然有效。 - splatch
1
surefire 2.21.0 也不行,必须回滚到 2.19.1。 - valodzka
3
经验证,maven-surefire-plugin v2.21.0junit-jupiter-engine v5.2.0junit-platform-surefire-provider v1.2.0 配合良好。 - Serhii Povísenko
5
这个配置在使用Surefire 2.22.0或更高版本时会失败。你需要从Surefire配置中排除Junit依赖项。我在这里写了一篇简短的文章 - https://springframework.guru/why-your-junit-5-tests-are-not-running-under-maven/ - John Thompson
2
junit-platform-surefire-provider已被弃用,并计划在JUnit Platform 1.4中删除。请改用Maven Surefire >= 2.22.0中的内置支持。 https://junit.org/junit5/docs/current/user-guide/#running-tests-build-maven - Saikat
显示剩余4条评论

79

更新2

问题已在Maven Surefire插件v2.22.0中修复。

新版本已经在Maven Central Repository上发布。

Maven

<dependency>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.22.0</version>
</dependency>
Gradle
compile group: 'org.apache.maven.plugins', name: 'maven-surefire-plugin', version: '2.22.0'

更新

正如Marian所指出的那样,最新版本的JUnit 5平台Surefire提供程序(1.2.0)支持最新版本的Maven Surefire插件(2.21.0)

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.21.0</version>
            <dependencies>
                <dependency>
                    <groupId>org.junit.platform</groupId>
                    <artifactId>junit-platform-surefire-provider</artifactId>
                    <version>1.2.0</version>
                </dependency>
            </dependencies>
        </plugin>



示例

pom.xml

<dependencies>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-engine</artifactId>
        <version>5.2.0</version>
        <scope>test</scope>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.21.0</version>
            <dependencies>
                <dependency>
                    <groupId>org.junit.platform</groupId>
                    <artifactId>junit-platform-surefire-provider</artifactId>
                    <version>1.2.0</version>
                </dependency>
            </dependencies>
        </plugin>
    </plugins>
</build>

TestScenario.java

package test;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

public class TestScenario {

    @Test
    @DisplayName("Test 2 + 2 = 4")
    public void test() {
        Assertions.assertEquals(4, 2 + 2);
    }
}

输出结果 (mvn clean install)

...
[INFO] --- maven-surefire-plugin:2.21.0:test (default-test) @ test --- [INFO]
[INFO] -------------------------------------------------------
[INFO] 测 试
[INFO] -------------------------------------------------------
[INFO] 正在运行 test.TestScenario
[INFO] 测试执行: 1, 失败: 0, 错误: 0, 跳过: 0, 耗时: 0.005 s - test.TestScenario
[INFO]
[INFO] 结果:
[INFO]
[INFO] 测试执行: 1, 失败: 0, 错误: 0, 跳过: 0
...


到目前为止最简单的方法:

    <plugin>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.19.1</version>
        <dependencies>
            <dependency>
                <groupId>org.junit.platform</groupId>
                <artifactId>junit-platform-surefire-provider</artifactId>
                <version>1.1.0</version>
            </dependency>
        </dependencies>
    </plugin>

1
这仅仅是因为类名中有 Test 才能正常工作。如果将类重命名为 ExampleScenario,则无法发现它。(Surefire 2.22.0) - Alexei Vinogradov
@AlexeiVinogradov 是的,这是预期的行为。更多信息请参见此处:https://dev59.com/ceo6XIcBkEYKwwoYSCpJ#6178629 - Mikhail Kholodkov
请注意:junit-jupiter-engine:5.1 依赖项不适用于 Spring Boot 版本。新版本的 JUnit 引擎可以使用。Spring Boot 仍预配置了 JUnit4 <junit.version>4.13。 - Wooff

25
JUnit 5文档中得知:
自版本2.22.0起,Maven Surefire提供本机支持,可在JUnit Platform上执行测试。
此外,您还可以在maven-surefire-plugin文档中阅读:
使用JUnit 5 Platform
要开始使用JUnit Platform,您需要至少向项目添加单个TestEngine实现。例如,如果您想使用Jupiter编写测试,则将测试工件junit-jupiter-engine添加到POM依赖项中。
因此,这就足以运行JUnit 5测试了。
<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>davidxxx</groupId>
    <artifactId>minimal-pom-junit5</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <properties>
        <junit-jupiter.version>5.2.0</junit-jupiter.version> 
        <!--optional below but good practice to specify our java version-->
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

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

        <!--optional below -->
        <!-- add any JUnit extension you need such as -->
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-params</artifactId>
            <version>${junit-jupiter.version}</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

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

</project>

我在我的 GitHub 空间中添加了一个可以浏览/克隆的工作示例 Maven 项目。
网址:https://github.com/ebundy/junit5-minimal-maven-project


20

我在2019年8月遇到了与此类似的问题,我在这里提问:Maven无声地无法找到要运行的JUnit测试。这些答案引导我朝正确的方向前进,但我发现你甚至可以更简洁地解决这个问题。我从JUnit5样例Maven项目中复制了我的解决方案。

截至JUnit 5.5.1和maven-surefire-plugin 2.22.2,您不需要添加junit-platform-surefire-provider依赖项。只需在pom.xml文件中指定一个插件和一个依赖项即可:

<dependencies>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter</artifactId>
        <version>5.5.1</version>
        <scope>test</scope>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.22.2</version>
        </plugin>
    </plugins>
</build>

3
这是正确且简洁的方法。适用于 OpenJDK 11 和 JUnit 5.6.2。 - k_rollo
2
非常适合我,其他选项都没有起作用。 - Carlos

9
我在使用JUnit5和Maven时遇到了这个问题,但我也注意到,即使只添加junit-jupiter-engine作为依赖项,在一些项目上测试也会运行,而在另一些项目上则不会。我在这里的评论中也看到了同样的模式:在@Alex的评论中,你可以看到他没有任何问题,即使是早期版本的surefire/junit/platform。
经过一段时间的思考,我意识到那些测试无法运行的项目是那些测试方法名称中不包含单词"test"的项目。虽然这并不是http://maven.apache.org/surefire/maven-surefire-plugin/examples/inclusion-exclusion.html所要求的。
换句话说:只需
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-engine</artifactId>
        <version>5.2.0</version>
        <scope>test</scope>
    </dependency>

这个。
@Test
public void something() {
    assertTrue(true);
}

将不会被运行,而

@Test
public void testSomething() {
    assertTrue(true);
}

将会运行!

这个问题像俄罗斯套娃一样展开......

无论如何,为@Mikhail Kholodkov加1,他的更新答案一次性解决了所有问题!


我最近也遇到了一个问题,使用mvn 3.5.0和jdk 1.8.0_101时,我的类名中没有'Test',导致测试未被检测到,这个经验让我受益匪浅! - dann.dev
是的,确实应该遵守http://maven.apache.org/surefire/maven-surefire-plugin/examples/inclusion-exclusion.html 但我说的是另外一件事。 - Fabien M
是的,事实证明我有两个问题,第一个是没有记住基本的Maven Surefire测试规则。第二个是没有执行Maven > 更新项目,以便Eclipse可以使用JUnit 5启动器。 - dann.dev
我想补充一点,类和方法必须是公共的。 - Jónás Balázs

6

补充一下,surefire 2.22.0 + junit 5.2.0 + platform 1.2.0 也可以使用。这里提供一个可行的pom供您参考:

    <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>org.jjhome.junit5</groupId>
    <artifactId>junit5-hone</artifactId>
    <packaging>jar</packaging>
    <version>1.0.0-SNAPSHOT</version>
    <name>junit5-home</name>

    <properties>
        <maven.compiler.source>1.6</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <junit5.version>5.2.0</junit5.version>
        <platform.version>1.2.0</platform.version>
        <surefire.version>2.22.0</surefire.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>${junit5.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-params</artifactId>
            <version>${junit5.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>${junit5.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.vintage</groupId>
            <artifactId>junit-vintage-engine</artifactId>
            <version>${junit5.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-launcher</artifactId>
            <version>${platform.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-runner</artifactId>
            <version>${platform.version}</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>${surefire.version}</version>
                <dependencies>
                    <dependency>
                        <groupId>org.junit.platform</groupId>
                        <artifactId>junit-platform-surefire-provider</artifactId>
                        <version>${platform.version}</version>
                    </dependency>
                    <dependency>
                        <groupId>org.junit.jupiter</groupId>
                        <artifactId>junit-jupiter-engine</artifactId>
                        <version>${junit5.version}</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>
</project>

1
我的问题是我没有将junit-vintage-engine作为依赖项,而且我所有的测试都是使用JUnit 4编写的。 - Mark
指定的JUnit、平台和Surefire版本的组合对我有效。谢谢! - CoderTR

6

这是因为类路径中存在TestNG (SUREFIRE-1527)所致。Groovy 2.5.5 POM加入了groovy-testng模块。

手动指定测试框架提供程序(如https://maven.apache.org/surefire/maven-surefire-plugin/examples/providers.html所述)可以解决该问题:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.22.1</version>

    <dependency>
        <groupId>org.apache.maven.surefire</groupId>
        <artifactId>surefire-junit-platform</artifactId>
        <version>2.22.1</version>
    </dependency>

1
你应该在代码示例中将<dependency>包装在<dependencies>中。 - user1053510
你救了我,谢谢...包含了groovy-all和groovy-testng,但在排除后...测试正在运行。 - pero_hero

3

我注意到一个问题,我成功地解决了它:

  • 将我的测试类命名为ClinicCalendarShould不会被maven识别
  • 将我的测试类命名为ClinicCalendarTest可以被maven识别

因此,除非我在surefire插件中缺少某种配置、参数或其他东西,否则默认情况下,你需要将你的测试类命名为XXXTest。


3

更新2022

以下内容现在可用:

    <plugins>
        <plugin>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.22.2</version>
        </plugin>
        <plugin>
            <artifactId>maven-failsafe-plugin</artifactId>
            <version>2.22.2</version>
        </plugin>
    </plugins>

当然还需要添加依赖项:

    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter</artifactId>
        <version>5.8.2</version>
        <scope>test</scope>
    </dependency>

现在检测到了测试。


看起来我们又回到了3.0.0-x的这个问题。 - dan

3

我遇到了同样的问题, junit5maven-surefire测试都失败了,但是junit4运行正常。以下组合适用于我,我不添加版本控制。使用junit-bom进行依赖管理。使用spring-boot 2.1.4

   <dependencyManagement>
    <dependencies>
            <dependency>
                <groupId>org.junit</groupId>
                <artifactId>junit-bom</artifactId>
                <version>5.6.1</version>
                <scope>import</scope>
                <type>pom</type>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <scope>test</scope>
        </dependency>

<build>
    <plugins>
        <plugin>
            <artifactId>maven-surefire-plugin</artifactId>
        </plugin>
    </plugins>
</build>

请确保升级到最新版本的Eclipse


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接