surefire不接受JUnit5测试

qnyhuwrf  于 2021-07-09  发布在  Java
关注(0)|答案(16)|浏览(488)

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

你知道怎么做吗?

vq8itlhq

vq8itlhq1#

正在更新到 maven-surefire-plugin:2.20 运行junit5测试没有问题。
但我用的是 M6 junit5上的版本。

cbwuti44

cbwuti442#

surefire 2.20存在未解决的问题
它是为我与冲浪2.19+junit平台-*1.0.3

mtb9vblg

mtb9vblg3#

我遇到了一个类似的问题,也导致surefire识别零测试。
我的问题与以下内容有关(来自junit 5.1.0/maven文档):
由于SureFire2.20内存泄漏和java 9上运行的问题,junit平台surefire提供程序目前仅适用于SureFire2.19.1。
我试图使用最新版本的surefire(2.21.0)和junit平台surefire provider(1.1.0),但它没有起作用(在java8或9中都没有)
切换回surefire2.19.1解决了我的问题。
根据这个问题,junit平台surefire provider的1.2.0版本中将包含一个修复程序(目前仅作为快照提供)。

hgtggwj0

hgtggwj04#

这个 maven-surefire-plugin 到今天为止,还没有完全支持JUnit5。在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>
9rygscc1

9rygscc15#

我面临着同样的问题 junit5 以及 maven-surefire 测试失败了。然而 junit4 一切正常。下面的组合对我有效,我不添加版本控制。使用 junit-bom 用于依赖关系管理。使用
spring-boot 2.1.4 ```



org.junit
junit-bom
5.6.1
import
pom





org.junit.jupiter
junit-jupiter-engine
test


org.junit.jupiter
junit-jupiter-api
test

ylamdve6

ylamdve66#

更新2

这个问题已经在maven surefire插件v2.22.0中修复
maven中央存储库提供了新版本。
Maven

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

grad尔

compile group: 'org.apache.maven.plugins', name: 'maven-surefire-plugin', version: '2.22.0'

更新

正如marian指出的,junit 5平台surefire provider(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>

测试方案.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·苏莱菲尔-plugin:2.21.0:test(默认测试)@test---[info]
[信息]

gorkyyrv

gorkyyrv7#

作为补充,surefire2.22.0+junit5.2.0+platform1.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>
ef1yzkbh

ef1yzkbh8#

在我的例子中,这是因为类路径中的testng(surefire-1527)。Groovy2.5.5POM带来了 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>
ltqd579y

ltqd579y9#

在我的例子中,surefire插件没有获得jupiter引擎/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插件,我也不是这样!
希望有帮助。。。

pbpqsu0x

pbpqsu0x10#

我在junit5和maven中遇到了这个问题,但也注意到,即使只添加junitjupiter引擎作为依赖项,测试也会在某些项目上运行,而不会在其他项目上运行。我在这里的评论中看到了相同的模式:在上面的@alex comment中,你可以看到他没有任何问题,即使是早期版本的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,他的更新答案一下子解决了所有问题!

fumotvh3

fumotvh311#

--
[信息]技术
[信息]

dojqjjoe

dojqjjoe12#

有一件事我注意到我能让它工作:
命名我的测试类 ClinicCalendarShould 不会被maven发现
命名我的测试类 ClinicCalendarTest 会被maven接走吗
因此,除非我在surefire插件中丢失了某种配置或参数,否则默认情况下,您需要将测试类命名为test。

wn9m85ua

wn9m85ua13#

来自junit 5文档:
从版本开始 2.22.0 maven surefire为在junit平台上执行测试提供本机支持。
此外,你还可以在 maven-surefire-plugin 文档:
使用JUnit5平台
要开始使用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

ua4mk5z4

ua4mk5z414#

--
[信息]运行test.testscenario
[信息]在test.testscenario中,测试运行:1,失败:0,错误:0,跳过:0,运行时间:0.005 s
[信息]
[信息]结果:
[信息]
[信息]测试运行: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>
suzh9iv8

suzh9iv815#

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

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

那么就不需要surefire插件了。

相关问题