java - Surefire 没有接受 Junit 5 测试

标签 java maven junit maven-surefire-plugin junit5

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

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

最佳答案

maven-surefire-plugin,从今天开始,not have full support of JUnit 5 .在 SUREFIRE-1206 中添加此支持存在一个 Unresolved 问题。 .

因此,您需要使用 custom provider . JUnit 团队已经开发了一个;来自 user guide ,您需要为新 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>

关于java - Surefire 没有接受 Junit 5 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36970384/

相关文章:

unit-testing - 在哪里测试私有(private)方法返回的对象?

java - 如何在java8中更改字符串列表中的项目

Maven <include> 通配符匹配部分文件夹名称

java - ant junit 任务不报告详细信息

java - Maven 项目在 IntelliJ 中构建得很好,但在 Maven 命令行中则不行

java - 如何使用 Liferay 和 Maven 高效地编写 portlet?

java - 用于单元测试的 Mock dao

java - 如何检查类是否有变量并在其他类中声明它

java - 有条件地从流中删除第一个(索引为零)元素

java - 启用 Spring Security 4 的 Spring MVC