141

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

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

4

18 回答 18

129

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

因此,您需要使用自定义提供程序。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>

此外,请务必声明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>
于 2016-05-01T18:42:54.337 回答
73

更新 2

问题已在 Maven Surefire Plugin v2.22.0 中修复

新版本在 Maven 中央存储库中可用。

马文

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

摇篮

compile group: 'org.apache.maven.plugins', name: 'maven-surefire-plugin', version: '2.22.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>



例子

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>

测试场景.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 全新安装)

...
[INFO] --- maven-surefire-plugin:2.21.0 :test (default-test) @ test --- [INFO]
[INFO] -------------- -----------------------------------------
[INFO] 测试
[INFO] -- -------------------------------------------------- ---
[INFO] Running 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>
于 2018-02-27T23:09:05.360 回答
25

JUnit 5 文档

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

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

使用 JUnit 5 平台

要开始使用 JUnit 平台,您需要向项目中添加至少一个 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

于 2018-08-14T09:35:23.623 回答
15

我在 2019 年 8 月遇到了同样的问题,我在这里问过这个问题:Maven 默默地找不到要运行的 JUnit 测试。这些答案使我朝着正确的方向前进,但我发现您可以更简洁地解决问题。我从JUnit5 示例 Maven 项目中复制了我的解决方案。

从 JUnit 5.5.1 和maven-surefire-plugin2.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>
于 2019-08-15T12:08:38.973 回答
9

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

在摸索了一段时间后,我意识到那些无法运行测试的项目是那些测试方法名称包含“测试”一词的项目。尽管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,其更新的答案立即解决了所有问题!

于 2018-05-25T10:06:30.960 回答
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>
于 2018-08-11T04:33:05.647 回答
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>
于 2019-03-21T14:22:28.007 回答
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

于 2020-03-29T16:53:15.017 回答
3

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

  • 命名我的测试类ClinicCalendarShould并没有被 maven 采纳
  • 命名我的测试类ClinicCalendarTest确实被 maven 拾取

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

于 2018-12-30T06:37:00.547 回答
1

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

我的问题原来与以下有关(来自JUnit 5.1.0 / maven文档):

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

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

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

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

于 2018-03-26T11:46:02.957 回答
1

Surefire 2.20 存在未解决的问题

它适用于我的 surfire 2.19 + junit-platform-* 1.0.3

于 2018-01-24T23:02:14.397 回答
0

就我而言,surefire 插件没有得到正确的 jupiter-engine/api 版本。即使运行 Maven 3.6.1 和 surefireplugin 版本 2.22.2 也是如此!

现在我的surefire插件配置看起来像:

<pluginManagement>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.22.2</version>
            <dependencies>
                <dependency>
                    <groupId>org.junit.jupiter</groupId>
                    <artifactId>junit-jupiter-engine</artifactId>
                    <version>5.5.2</version>
                </dependency>
            </dependencies>
        </plugin>

        ...
    </plugins>
</pluginManagement>

此外,我不得不强制这些版本:

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>5.5.2</version>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.5.2</version>
        </dependency>
        <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-engine</artifactId>
            <version>1.5.2</version>
        </dependency>
        <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-commons</artifactId>
            <version>1.5.2</version>
        </dependency>
    </dependencies>
</dependencyManagement>

在我的情况下,看起来 5.5.2 链接到了错误的平台版本 1.3.2 而不是 1.5.2。

所有的 JUnit5 测试现在都被拾起。即使使用 2.22.0 的 surefire 插件,这对我来说也不是这样!

希望有帮助...

于 2019-10-23T09:54:32.917 回答
0

小心嵌套测试。我有一个这样的测试课:

class AcrTerminalTest {
    @Nested
    class card_is_absent {
        AcrTerminal acrTerminal = new AcrTerminal(new CardAbsentFakeCardTerminal());

        @Test
        void isCardPresent_returns_false() {
            assertThat(acrTerminal.isCardPresent())
                    .isFalse();
        }
    }
}

运行./mvnw test -Dtest=AcrTerminalTest失败,我需要*像这样在测试类名之后添加./mvnw test -Dtest=AcrTerminalTest\*(见星号)。

于 2021-06-01T18:53:15.520 回答
0

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>

现在检测到测试。

于 2022-01-07T14:54:10.047 回答
0

schnell18在https://stackoverflow.com/a/51796487/1619489上方的一些行中对此进行了评论

对我来说很容易:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.5.5</version>
</parent>

<build>
    <finalName>app-console-services</finalName>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-failsafe-plugin</artifactId>
        </plugin>
    </plugins>
</build>

...而这种依赖...

<dependency>
    <groupId>org.junit.vintage</groupId>
    <artifactId>junit-vintage-engine</artifactId>
    <scope>test</scope>
</dependency>
于 2021-11-30T19:31:47.447 回答
0

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

但我使用的M6是 Junit5 上的版本。

于 2017-08-04T08:31:12.153 回答
0

根据我的案例经验, pom.xml 中的库junit-jupiter-engine应该没有依赖关系。然后可以使用插件 maven-surefire-plugin 并依赖最新版本的junit-jupiter

于 2022-01-25T01:01:46.477 回答
-1

对我来说,解决方案是在类的顶部添加下面的注释。

@RunWith(JUnitPlatform.class)
public class MyTest {
 ....
}

然后甚至不需要surefire插件。

于 2020-12-09T05:21:12.867 回答