【问题标题】:Maven not running JUnit 5 testsMaven 没有运行 JUnit 5 测试
【发布时间】:2019-04-25 07:08:09
【问题描述】:

我正在尝试使用 maven 运行一个简单的 junit 测试,但它没有检测到任何测试。我哪里错了?项目目录

Project -> src -> test-> java -> MyTest.java

结果:

Tests run: 0, Failures: 0, Errors: 0, Skipped: 0

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.buildproftest.ecs</groupId>
    <artifactId>buildprofiletest</artifactId>
    <version>1.0-SNAPSHOT</version>
    
    <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.3.1</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
                <configuration>
                    <debug>false</debug>
                    <optimize>true</optimize>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

Junit 测试用例

import org.junit.jupiter.api.Test;

public class MyTest {
    @Test
    public void printTest() {
        System.out.println("Running JUNIT test");
    }
}

响应是没有要运行的测试用例。

【问题讨论】:

    标签: maven junit maven-surefire-plugin junit5


    【解决方案1】:

    根据注释 (import org.junit.jupiter.api.Test),您正在尝试使用 Maven 运行 JUnit 5 测试。根据documentation,你必须添加这个依赖:

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

    您的 Maven 版本附带不支持 JUnit 5 的 maven-surefire-plugin 版本。您可以将 Maven 更新到最新版本。你也可以设置maven-surefire-plugin的版本:

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <!-- JUnit 5 requires Surefire version 2.22.0 or higher -->
        <version>2.22.0</version>
    </plugin>
    

    请参阅junit5-samples for this information

    查看 Maven 存储库中的 Maven Surefire Plugin 工件。截至 2019 年 1 月的版本为 3.0.0-M3

    【讨论】:

    • 我添加了依赖项并运行了 mvn clean test 但仍然没有运行测试。谢谢。
    • 你为什么还要依赖 JUnit 4.12?这可能是问题所在。尝试删除它。
    • 我现在已经删除了它,但仍然没有乐趣。我通过独立于 maven 的 IDE 运行了测试,它确实运行了。谢谢
    • 我可以重现该问题并使用maven-surefire-plugin 的版本修复它。
    • 提示:从 JUnit 5 版本 5.4.0 开始,您可以使用名为 JUnit Jupiter (Aggregator) 的新的方便的单个 Maven 工件,从而获得所需的多个相关工件编写和运行 JUnit 5 测试:junit-jupiter
    【解决方案2】:

    junit-jupiter — JUnit 5 的更简单原型

    Answer by LaurentG 似乎是正确的,但有点过时了。

    从 JUnit 5.4 开始,您可以替换那些多个 Maven 工件

    • junit
    • junit-jupiter-api
    • junit-jupiter-engine

    …使用单个工件:

    …运行 JUnit 5 测试。

    这个新工件是其他工件的集合,是简化 POM 文件的便捷包装器。

        <dependencies>
    
            <!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter -->
            <!-- Provides everything you need to write JUnit 5 Jupiter tests. -->
            <dependency>
                <groupId>org.junit.jupiter</groupId>
                <artifactId>junit-jupiter</artifactId>
                <version>5.7.0-M1</version>
                <scope>test</scope>
            </dependency>
            
        </dependencies>
    

    这为您提供了编写和运行 JUnit 5 Jupiter 测试所需的一切。

    junit-vintage-engine 用于 JUnit 3 和 4 测试

    如果您想要继续运行旧的 JUnit 3 或 JUnit 4 legacy 测试,请添加第二个依赖项 junit-vintage-engine

    <dependencies>
    
        <!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter -->
        <!-- Provides everything you need to write JUnit 5 Jupiter tests. -->
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter</artifactId>
            <version>5.7.0-M1</version>
            <scope>test</scope>
        </dependency>
    
        <!-- https://mvnrepository.com/artifact/org.junit.vintage/junit-vintage-engine -->
        <!-- Enables any legacy JUnit 3 and JUnit 4 tests you may have. Not needed for JUnit 5 tests. -->
        <dependency>
            <groupId>org.junit.vintage</groupId>
            <artifactId>junit-vintage-engine</artifactId>
            <version>5.7.0-M1</version>
            <scope>test</scope>
        </dependency>
    
    </dependencies>
    

    maven-surefire-plugin

    您还需要Surefire plugin,如other Answer 所示。请务必获取最新版本,因为 Surefire 最近进行了一些重要的修复/增强。当前版本为 3.0.0-M5。

    【讨论】:

    • +1 要求 junit-vintage 运行传统的 Junit 3 / 4 测试 - 升级了surefire和junit,但我的测试没有得到执行。引入 junit-vintage 依赖就成功了
    猜你喜欢
    • 2021-12-16
    • 2020-12-31
    • 2013-08-23
    • 1970-01-01
    • 1970-01-01
    • 2018-09-02
    • 1970-01-01
    • 1970-01-01
    • 2020-09-18
    相关资源
    最近更新 更多