Java Surefire 不接受 Junit 5 测试

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36970384/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 18:53:04  来源:igfitidea点击:

Surefire is not picking up Junit 5 tests

javamavenjunitmaven-surefire-pluginjunit5

提问by Ali Dehghani

I wrote a simple test method with JUnit 5:

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

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

But when I run mvn test, I got:

但是当我运行时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

Somehow, surefire didn't recognize that test class. My pom.xmllooks like:

不知何故,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>

Any idea how to make this work?

知道如何进行这项工作吗?

采纳答案by Tunaki

The maven-surefire-plugin, as of today, does not have full support of JUnit 5. There is an open issue about adding this support in SUREFIRE-1206.

maven-surefire-plugin,从今天开始,并没有完全支持JUnit的5。在SUREFIRE-1206 中添加此支持存在一个未解决的问题。

As such, you need to use a custom provider. One has already been developed by the JUnit team; from the user guide, you need to add the junit-platform-surefire-providerprovider and the TestEngineimplementation for the new API:

因此,您需要使用自定义 provider。一种已经由 JUnit 团队开发;从用户指南中,您需要为新 API添加junit-platform-surefire-provider提供程序和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>

Also, be sure to declare the junit-jupiter-apidependency with a scope of test:

另外,请务必声明junit-jupiter-api范围为的依赖项test

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

回答by LazerBanana

Updating to maven-surefire-plugin:2.20runs the Junit5 tests with no problem.

更新maven-surefire-plugin:2.20运行 Junit5 测试没有问题。

But I am using the M6version on Junit5.

但我M6在 Junit5 上使用该版本。

回答by Alex

There is open issue for surefire 2.20

万无一失 2.20 存在未解决的问题

It is working for me with surfire 2.19 + junit-platform-* 1.0.3

它对我有用 surfire 2.19 + junit-platform-* 1.0.3

回答by Mikhail Kholodkov

Update 2

更新 2

Issuehas been fixed in Maven Surefire Plugin v2.22.0

Maven Surefire Plugin v2.22.0 已修复问题

New versionis available at Maven Central Repository.

新版本可在 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'


Update

更新

As Marianpointed out, the latest version of JUnit 5 Platform Surefire Provider (1.2.0)supports latest version of Maven Surefire Plugin (2.21.0):

正如玛丽安所指出的,最新版本的JUnit 5 Platform Surefire Provider (1.2.0)支持最新版本的Maven Surefire Plugin (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>





Example

例子

pom.xml

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

测试场景.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);
    }
}

Output (mvn clean install)

输出(mvn 全新安装)

...
[INFO] --- maven-surefire-plugin:2.21.0:test (default-test) @ test --- [INFO]
[INFO] -------------------------------------------------------
[INFO] T E S T S
[INFO] -------------------------------------------------------
[INFO] Running test.TestScenario
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.005 s - in test.TestScenario
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
...

...
[INFO] --- maven-surefire-plugin:2.21.0:test (default-test) @test--- [INFO]
[INFO] -------------- -----------------------------------------
[信息] 测试
[信息] -- -------------------------------------------------- ---
[INFO] 运行 test.TestScenario
[INFO] 测试运行:1,失败:0,错误:0,跳过:0,经过的时间:0.005 秒 - 在 test.TestScenario
[INFO]
[INFO] 结果:
[INFO ] ]
[信息]测试运行:1,失败:0,错误:0,跳过:0
...



Simplest way till today:

直到今天最简单的方法:

    <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>

回答by avandeursen

I had a similar problem also causing Surefire to recognize zero tests.

我有一个类似的问题也导致 Surefire 识别零测试。

My problem turned out to be related to the following (from the JUnit 5.1.0 / mavendocumentation):

结果证明我的问题与以下内容有关(来自JUnit 5.1.0 / maven文档):

Due to a memory leak in Surefire 2.20 and issues running on Java 9, the junit-platform-surefire-provider currently only works with Surefire 2.19.1.

由于 Surefire 2.20 中的内存泄漏以及在 Java 9 上运行的问题,junit-platform-surefire-provider 目前仅适用于 Surefire 2.19.1。

I was trying to use the latest versions of Surefire (2.21.0) and junit-platform-surefire-provider (1.1.0), and it did not work (in neither Java 8 or 9)

我试图使用最新版本的 Surefire (2.21.0) 和 junit-platform-surefire-provider (1.1.0),但它不起作用(在 Java 8 或 9 中)

Switching back to Surefire 2.19.1 solved my problem.

切换回 Surefire 2.19.1 解决了我的问题。

According to this issuea fix will be included in version 1.2.0 of the junit-platform-surefire-provider (currently available as SNAPSHOT only).

根据此问题,junit-platform-surefire-provider 的 1.2.0 版将包含一个修复程序(目前仅作为 SNAPSHOT 提供)。

回答by Fabien M

I ran into this issue with JUnit5 and Maven but also noticed that, even if onlyjunit-jupiter-engine was added as a dependency, tests would run on some projects, not on others. And I kind of see the same pattern in the comments here: In @Alex comment above you can see he doesn't have any issue, even with earlier versions of surefire/junit/platform.

我在使用 JUnit5 和 Maven 时遇到了这个问题,但也注意到,即使只添加了junit-jupiter-engine 作为依赖项,测试也会在某些项目上运行,而不是在其他项目上运行。我在这里的评论中看到了相同的模式:在上面的@Alex 评论中,您可以看到他没有任何问题,即使使用早期版本的 surefire/junit/platform。

After scratching my head for some time I realized that those projects where the tests wouldn't run were those where the tests methodnames dit notcontain the word "test". Though this isn't mandated by http://maven.apache.org/surefire/maven-surefire-plugin/examples/inclusion-exclusion.html

在摸索了一段时间后,我意识到那些不会运行测试的项目是那些测试方法名称包含“测试”一词的项目。虽然这不是由http://maven.apache.org/surefire/maven-surefire-plugin/examples/inclusion-exclusion.html强制要求的

In other words: just with

换句话说:只要

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

this

这个

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

will NOT be run, whereas

不会运行,而

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

WILL be run !

将运行!

This issue unfolds as a russian doll...

这个问题以俄罗斯娃娃的形式展开……

Anyway, +1 for @Mikhail Kholodkov whose updated answer fixes all the issues at once!

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

回答by schnell18

Just to complement, surefire 2.22.0 + junit 5.2.0 + platform 1.2.0 also works. Attached is a working pom for your referecne:

作为补充,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>

回答by davidxxx

From the JUnit 5 documentation:

JUnit 5 文档

Starting with version 2.22.0, Maven Surefire provides native support for executing tests on the JUnit Platform.

从 version 开始2.22.0,Maven Surefire 为在 JUnit 平台上执行测试提供本机支持。

Additionally you can read in the maven-surefire-plugindocumentation:

此外,您可以在maven-surefire-plugin文档中阅读:

Using JUnit 5 Platform

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

使用 JUnit 5 平台

要开始使用 JUnit 平台,您至少需要向项目添加一个 TestEngine实现。例如,如果你想用 Jupiter 编写测试,将测试工件添加junit-jupiter-engine到 POM 中的依赖项

So just that is enough to make run JUnit 5 tests :

所以这足以运行 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>

On my GitHub space I added a working sample maven project that you can browse/clone.
URL: https://github.com/ebundy/junit5-minimal-maven-project

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

回答by jkilgrow

One thing I noticed that I was able to get it working:

我注意到我能够让它工作的一件事:

  • Naming my test class ClinicCalendarShoulddoes not get picked up by maven
  • Naming my test class ClinicCalendarTestDOES get picked up by maven
  • 命名我的测试类ClinicCalendarShould没有被 maven 接收
  • 命名我的测试类ClinicCalendarTest确实会被 maven 接收

So, unless I am missing some sort of configuration or parameter or whatever in the surefire plugin, by default, you need to name your test classes XXXTest.

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

回答by Lu55

In my case this was because of the TestNG in the classpath (SUREFIRE-1527). Groovy 2.5.5 POM have brought it with the groovy-testngmodule.

在我的情况下,这是因为类路径中的 TestNG ( SUREFIRE-1527)。Groovy 2.5.5 POM 带来了它的groovy-testng模块。

Manually specified test-framework provider (as it's described at the https://maven.apache.org/surefire/maven-surefire-plugin/examples/providers.html) solved the problem:

手动指定的测试框架提供程序(如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>