Maven jUnit5 org.junit.platform.runner, org.junit.platform.suite.api 不存在

Maven jUnit5 org.junit.platform.runner, org.junit.platform.suite.api does not exist

使用 Eclipse。 能够从 Eclipse IDE 启动测试,但是 'maven install' 失败并抛出:

package org.junit.platform.runner does not exist
package org.junit.platform.suite.api does not exist
package org.junit.runner does not exist
cannot find symbol  symbol: class RunWith
cannot find symbol  symbol: class SelectClasses

这些是我的 pom.xml 文件依赖项

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

为什么我的 IDE 能够启动测试脚本和程序本身,但是 maven 构建失败并抛出上述错误?

看起来我错过了几个依赖项(必须添加 junit-platform-launcher 和 junit-platform-运行ner),也是为了在每个构建上对 运行 进行测试添加构建插件。现在我的 pom.xml 看起来像这样:

<properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>

        <junit.jupiter.version>5.5.2</junit.jupiter.version>
        <junit.platform.version>1.5.2</junit.platform.version>
    </properties>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.2</version>
            </plugin>
            <plugin>
                <artifactId>maven-failsafe-plugin</artifactId>
                <version>2.22.2</version>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>${junit.jupiter.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>${junit.jupiter.version}</version>
            <scope>test</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.junit.platform/junit-platform-runner -->
        <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-runner</artifactId>
            <version>${junit.platform.version}</version>
            <scope>test</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.junit.platform/junit-platform-launcher -->
        <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-launcher</artifactId>
            <version>${junit.platform.version}</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

将 Jupiter 与 Maven 一起使用只需要一个依赖项,如下所示:https://github.com/junit-team/junit5-samples/tree/r5.5.2/junit5-jupiter-starter-maven

surefire 插件将解决您的问题。在结束项目标签之前,只需在您的 pom 中添加以下内容。添加依赖项后,您可以 运行 在命令行 "mvn test" 或 "mvn install"

<build>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>3.0.0-M4</version>
            </plugin>
        </plugins>
    </pluginManagement>
</build>