【问题标题】:Surefire 2.22.2 is not finding junit tests after removal of provider dependencySurefire 2.22.2 在删除提供程序依赖项后未找到 junit 测试
【发布时间】:2020-10-02 21:30:14
【问题描述】:

第一次发帖!我已尝试浏览所有其他有关万无一失问题的帖子,但无济于事,因此将不胜感激。

我目前正在尝试处理我的项目中的“junit-platform-surefire-provider 已被弃用”警告。正在使用 2.22.2 版的 surefire,目前我们项目中的所有 junit jupiter 测试都已在该版本中找到。

警告: junit-platform-surefire-provider 已被弃用并计划 在 JUnit Platform 1.4 中被删除。请使用 Maven 中的内置支持 Surefire >= 2.22.0 代替。 https://junit.org/junit5/docs/current/user-guide/#running-tests-build-maven

Test results with 55 tests found

当我尝试从插件中删除 junit-platform-surefire-provider 和 jupiter-engine 依赖项时出现问题。

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.22.2</version>
            <dependencies>
                <dependency>
                    <groupId>org.junit.platform</groupId>
                    <artifactId>junit-platform-surefire-provider</artifactId>
                    <version>1.3.2</version>
                </dependency>

                <dependency>
                    <groupId>org.junit.jupiter</groupId>
                    <artifactId>junit-jupiter-engine</artifactId>
                    <version>${junit-jupiter.version}</version>
                </dependency>
            </dependencies>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-failsafe-plugin</artifactId>
            <version>2.22.2</version>
        </plugin>

此提供程序将被弃用,我正在尝试摆脱它。我遵循https://junit.org/junit5/docs/current/user-guide/#running-tests-build-maven 的文档,以尝试使用 Maven Surefire 中的内置支持。除了删除这两个依赖项之外,我还在 POM 文件的部分中添加了以下两个依赖项。

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

完成此操作后,构建将完成,但不再找到任何测试。

Test Results with ZERO tests found

我有什么遗漏的吗?

【问题讨论】:

标签: java maven junit junit5 maven-surefire-plugin


【解决方案1】:

我遇到了类似的问题,我需要使用 Junit 5 长时间运行 Junit 4.12 测试。添加 Junit 5 测试后,我得到了下一个错误:

[INFO]  T E S T S 16:28:24 [INFO]
------------------------------------------------------- 
16:28:26 [INFO]  
16:28:26 [INFO] Results: 
16:28:26 [INFO]  
16:28:26 [INFO] Tests run: 0, Failures: 0, Errors: 0, Skipped: 0 
16:28:26 [INFO]  
16:28:26 [INFO]
------------------------------------------------------------------------ 
16:28:26 [INFO] BUILD SUCCESS 
16:28:26 [INFO]
------------------------------------------------------------------------

我读到添加

<dependency>
    <groupId>org.junit.platform</groupId>
    <artifactId>junit-platform-surefire-provider</artifactId>
    <version>1.3.2</version>
</dependency>

可以解决我的问题,但是当我尝试运行测试时,我收到了下一条消息

 [INFO]  T E S T S
    16:12:57 [INFO] -------------------------------------------------------
    16:12:58 
    16:12:58  +-------------------------------------------------------------------------------+
    16:12:58  | WARNING:                                                                      |
    16:12:58  | The junit-platform-surefire-provider has been deprecated and is scheduled to  |
    16:12:58  | be removed in JUnit Platform 1.4. Please use the built-in support in Maven    |
    16:12:58  | Surefire >= 2.22.0 instead.                                                   |
    16:12:58  | » https://junit.org/junit5/docs/current/user-guide/#running-tests-build-maven |
    16:12:58  +-------------------------------------------------------------------------------+
    16:12:58 

最后下一个 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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    .... More code here non relevant

    <dependencies>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
      
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.6.0</version>
             <scope>test</scope>
        </dependency>
      
        <dependency>
            <groupId>org.junit.vintage</groupId>
            <artifactId>junit-vintage-engine</artifactId>
            <version>5.5.2</version>
        </dependency>
    
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>5.6.0</version>
        </dependency>

    .... More code here non relevant

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.1</version>
                <configuration>
                    <dependenciesToScan>
                        <dependency>${group.id}:${test.project.name}</dependency>
                    </dependenciesToScan>
                    <encoding>UTF-8</encoding>
                    <inputEncoding>UTF-8</inputEncoding>
                    <outputEncoding>UTF-8</outputEncoding>
                    <argLine>-Dfile.encoding=UTF-8</argLine>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <configuration>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

现在,当我尝试运行 Junit 4.12 测试时,它们被找到并执行了。


更新

我注意到 log4j 日志存在一些问题,它只显示异常堆栈跟踪。 Junit5也停止工作。因此,经过更多测试我的 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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    .... More code here non relevant

    <dependencies>

        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>5.6.0</version>
        </dependency>

        <dependency>
            <groupId>org.junit.vintage</groupId>
            <artifactId>junit-vintage-engine</artifactId>
            <version>5.5.2</version>
        </dependency>
      
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>

    .... More code here non relevant

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>3.0.0-M3</version>
                <configuration>
                    <dependenciesToScan>
                        <dependency>${group.id}:${test.project.name}</dependency>
                    </dependenciesToScan>
                    <encoding>UTF-8</encoding>
                    <inputEncoding>UTF-8</inputEncoding>
                    <outputEncoding>UTF-8</outputEncoding>
                    <argLine>-Dfile.encoding=UTF-8</argLine>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <configuration>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

【讨论】:

    【解决方案2】:

    更新

    遇到同样的问题

    • Maven 3.6.0
    • Maven-surefire-plugin 2.22.2
    • JUnit 5.7.0
    • Java jdk 1.8.261

    添加到 pom.xml

    <dependencies>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.7.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.vintage</groupId>
            <artifactId>junit-vintage-engine</artifactId>
            <version>5.7.0</version>
        </dependency>    
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>5.7.0</version>
       </dependency>
       <!--.... other dependencies -->
    </dependencies>
    
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.2</version>
            </plugin>
            <!--.... other plugins -->
        </plugins>
    </build>
    

    工作正常并运行我的所有测试

    【讨论】:

    • 谢谢它真的为我节省了很多压力和时间!顺便说一句,不需要 junit-jupiter-api,因为它是 junit-jupiter-engine 的依赖项
    猜你喜欢
    • 1970-01-01
    • 2019-05-25
    • 1970-01-01
    • 2012-05-16
    • 1970-01-01
    • 2014-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多