Maven没有运行JUnit 5测试

mog*_*oli 6 junit maven maven-surefire-plugin junit5

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

Project -> src -> test-> java -> MyTest.java
Run Code Online (Sandbox Code Playgroud)

结果:

Tests run: 0, Failures: 0, Errors: 0, Skipped: 0
Run Code Online (Sandbox Code Playgroud)

的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>
Run Code Online (Sandbox Code Playgroud)

Junit测试用例

import org.junit.jupiter.api.Test;

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

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

感谢您的见解

小智 12

  • 我遇到了类似的问题,junit@Test注释可以工作,但 org.junit.jupiter.api.Test@Test注释不起作用。

解决方案:

  • 是专家官方指南,介绍如何配置maven-surefire-plugin并确保使用正确的junit-jupiter-engine

  • 该指南有点令人困惑,所以这就是我让它工作的方式

1)将引擎依赖添加到依赖块中:

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

Run Code Online (Sandbox Code Playgroud)

2)将引擎依赖添加到maven-surefire:

<plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>3.0.0-M6</version>
            <dependencies>
                <dependency>
                    <groupId>org.junit.jupiter</groupId>
                    <artifactId>junit-jupiter-engine</artifactId>
                    <version>5.6.2</version>
                </dependency>
            </dependencies>
        </plugin>

Run Code Online (Sandbox Code Playgroud)


Lau*_*ntG 11

根据annotation(import org.junit.jupiter.api.Test),您尝试使用Maven运行JUnit 5测试.根据文档,您必须添加此依赖项:

<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-engine</artifactId>
    <version>5.3.1</version>
    <scope>test</scope>
</dependency>
Run Code Online (Sandbox Code Playgroud)

您的Maven版本附带的版本maven-surefire-plugin不支持JUnit 5.您可以将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>
Run Code Online (Sandbox Code Playgroud)

有关此信息,请参阅junit5-samples.

请参阅Maven存储库中的Maven Surefire插件工件.截至3.0.0-M32019-01的版本.

  • 我添加了依赖项并运行了 mvn clean test 但仍然没有测试运行。谢谢。 (2认同)
  • 提示:从 JUnit 5 版本 5.4.0 开始,您可以使用名为 *JUnit Jupiter (Aggregator)* 的新的方便的单个 Maven 工件,它反过来为您提供编写和运行 JUnit 5 测试所需的几个相关工件:[`junit-jupiter `](https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter) (2认同)

Bas*_*que 5

junit-jupiter —更简单的原型

LaurentG答案似乎是正确的,但有些过时了。

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

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

…只有一个工件:

此新工件是其他工件的汇总,可以方便地包装以简化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.5.0-M1</version>
            <scope>test</scope>
        </dependency>

    </dependencies>
Run Code Online (Sandbox Code Playgroud)

这为您提供了编写和运行JUnit 5 Jupiter测试所需的全部功能。

如果您要继续运行旧的JUnit 3或JUnit 4 旧式测试,请添加第二个依赖项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.5.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.5.0-M1</version>
        <scope>test</scope>
    </dependency>

</dependencies>
Run Code Online (Sandbox Code Playgroud)

您还需要Surefire插件,如该其他Answer中所示。一定要获取最新版本,因为Surefire最近已进行了一些重要的修复/增强。当前版本为3.0.0-M3。

  • +1 要求 junit-vintage 运行旧版 Junit 3 / 4 测试 - 已升级 Surefire 和 junit 但我的测试尚未执行。引入 junit-vintage 依赖就成功了 (2认同)