有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Surefire 2.22.2在删除提供程序依赖项后找不到junit测试

第一次发布!我已经试着浏览了所有其他关于surefire问题的帖子,但没有任何结果,所以如果有任何帮助,我将不胜感激

我目前正在尝试处理项目中的“junit平台surefire提供程序已被弃用”警告。目前正在使用surefire的2.22.2版本,我们的项目中的所有junit jupiter测试都可以在该版本中找到

警告: junit平台surefire提供程序已被弃用,并计划 在JUnit平台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平台surefire提供程序和jupiter引擎依赖项时,问题就出现了

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

这个提供者将被弃用,我正试图摆脱它。为了尝试使用Maven Surefire中的内置支持,我遵循了https://junit.org/junit5/docs/current/user-guide/#running-tests-build-maven的文档。除了删除这两个依赖项之外,我还将以下两个依赖项添加到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

有什么我遗漏的吗


共 (2) 个答案

  1. # 1 楼答案

    更新

    也有同样的问题

    • Maven 3.6.0
    • Maven surefire插件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>
    

    工作正常,运行所有测试

  2. # 2 楼答案

    我有一个类似的问题,我需要用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 
    

    最后,next 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>