Surefire 没有接受 Junit 5 测试

新手上路,请多包涵

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

知道如何使这项工作吗?

原文由 Ali Dehghani 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 916
2 个回答

截至今天, maven-surefire-plugin没有完全支持 JUnit 5 。关于在 SUREFIRE-1206 中添加此支持存在未解决的问题。

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

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

原文由 Tunaki 发布,翻译遵循 CC BY-SA 3.0 许可协议

更新 2

问题 已在 Maven Surefire 插件 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'


更新

正如 Marian 指出的那样,最新版本的 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 全新安装)

[信息] --- maven-surefire-plugin:2.21.0 :test (default-test) @ test — [信息]

[信息]-------------------------------------------- ———-

[信息] 测试

[信息]-------------------------------------------- ———-

[信息] 运行测试.TestScenario

[信息] 测试运行:1,失败:0,错误:0,跳过:0,经过的时间:0.005 秒 - 在 test.TestScenario 中

[信息]

[信息] 结果:

[信息]

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

原文由 Mikhail Kholodkov 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题