【问题标题】:Maven Surefire Plugin does not attempt to run JUnit 5 TestsMaven Surefire 插件不会尝试运行 JUnit 5 测试
【发布时间】:2020-07-01 17:23:50
【问题描述】:

我正在尝试配置 Java Maven 项目以在构建时运行 JUnit 5 测试。

我有一个 Java 项目的标准文件结构,我的项目包含一个 src/main/java 文件夹,其中包含几个构建良好的文件,我最近为 JUnit 测试添加了一个 src/test/java 文件夹。

src/test/java 中,我有一个包含 JUnit 测试的文件,如下所示:

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class SampleTests {

    @Test
    @DisplayName("This Test Should fail")
    public void testThatAlwaysFails() {
        assertEquals(3, 2);
    }
}

以下是我的pom.xml 文件的相关部分:

<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    ...
    <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.6.2</junit.jupiter.version>
    </properties>
    <dependencies>
        ...
        <!-- For JUnit 5-->
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter</artifactId>
            <version>${junit.jupiter.version}</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <!-- For Running the JUnits-->
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.2</version>
            </plugin>
        </plugins>
    </build>
</project>

这一切似乎都符合我找到的文档以及其他 SO 帖子中的解决方案。但是,当我通过mvn clean install 构建项目时,这些是与测试相关的唯一输出行:

[INFO] --- maven-compiler-plugin:3.8.1:testCompile (groovy-tests) @ <project-name-here> ---
[INFO] Changes detected - recompiling the module!
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-surefire-plugin:2.22.2:test (default-test) @ <project-name-here> ---
[INFO] 
[INFO] --- maven-jar-plugin:3.2.0:jar (default-jar) @ <project-name-here> ---

(我已经从输出中删除了实际的项目名称,但正如预期的那样)。

这是什么原因,我如何配置我的pom 文件以便运行测试?我希望在maven-surefire-plugin 行之后有一行输出选择提供程序,然后它会说它找到了哪些测试和结果,但是这些都没有出现在控制台中。我已经为 JUnit Jupiter 和 Surefire 插件尝试了许多版本,同样的事情也发生了。我相信我曾经使用 JUnit 5 和 Surefire 插件的较旧设置让它工作,但我不记得版本,如果可能的话,我希望它使用这些依赖项的最新版本工作。

如果有什么不同,我也会从 IntelliJ IDEA Maven 配置中运行 mvn clean install

【问题讨论】:

标签: java maven junit5 maven-surefire-plugin


【解决方案1】:

我在 maven 文档中找到了以下提示,并成功运行了 Junit Tests。 这里是Running Tests

为了使用不同的 JUnit 5 版本(例如 5.7.0),您可能需要在其中包含相应版本的 junit-platform-launcher、junit-jupiter-engine 和 junit-vintage-engine类路径。

其他 Maven 依赖项

<!-- Only needed to run tests in a version of IntelliJ IDEA that bundles older versions -->
<dependency>
    <groupId>org.junit.platform</groupId>
    <artifactId>junit-platform-launcher</artifactId>
    <version>1.7.0</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-engine</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>
    <scope>test</scope>
</dependency>

添加上述jar文件后,maven test成功运行测试代码

【讨论】:

    猜你喜欢
    • 2020-12-31
    • 1970-01-01
    • 2018-09-02
    • 2019-09-30
    • 1970-01-01
    • 2021-05-16
    • 1970-01-01
    • 2020-07-25
    • 2019-04-25
    相关资源
    最近更新 更多