java - Maven 没有找到以 "Test"结尾的测试文件?

标签 java maven junit maven-surefire-plugin junit-jupiter

我将 jupiter、mockito 和 jupiter-engine 依赖项添加到 pom.xml 文件中,如您在以下剪贴板中所见:

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

    <groupId>com.example.myapp</groupId>
    <artifactId>testingit</artifactId>
    <version>1.0</version>
    <packaging>jar</packaging>
    
    <dependencies>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.9.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-inline</artifactId>
            <version>4.6.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>5.4.0</version>
        </dependency>
    </dependencies>
    
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>8</source>
                    <target>8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.1</version>
            </plugin>
        </plugins>
    </build>
</project>

我的测试在 src/test/java/com/example/myapp/testingit 包下:enter image description here

如果我运行 mvn test 它没有找到任何测试:
enter image description here

虽然我有测试:
enter image description here

出了什么问题?

最佳答案

您缺少 junit-platform-surefire-provider 依赖项。此外,您的 junit-jupiter-apijunit-jupiter-engine 版本不匹配,这可能并不重要,但可能是一个坏主意,会导致困难- 稍后查找错误。您可以使用聚合 junit-jupiter 依赖项来解决这两个问题:

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

    <groupId>com.example.myapp</groupId>
    <artifactId>testingit</artifactId>
    <version>1.0</version>
    <packaging>jar</packaging>

    <dependencies>
        <dependency> <!-- Here! -->           
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter</artifactId>
            <version>5.9.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-inline</artifactId>
            <version>4.6.1</version>
            <scope>test</scope>
        </dependency>
   </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>8</source>
                    <target>8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.2</version>
            </plugin>
        </plugins>
    </build>
</project>

关于java - Maven 没有找到以 "Test"结尾的测试文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74853920/

相关文章:

java - 使泛型函数适用于多个非重叠类型

java - 如何在java中实例化处理程序

java - 通过 hudson 制作耳朵和构建

maven - 使用属性从 Maven 部署到 JFrog Artifactory

java - JUnit 类别不适用于 TestCase?

java - 小程序。 java.lang.reflect.InvocationTargetException异常

java - 根据条件更新数组列表

java - Spring Initializr 项目无法运行并出现多个错误

java - 在 Eclipse 中使用不同的构建路径进行单元测试

java - 如何测试具有私有(private)方法、字段或内部类的类?