Maven无法运行JUnit 5测试。

91

我正在尝试使用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");
    }
}

回应是没有测试用例可供运行。

很好,你的测试方法是公共的,但是类可以对Junit 5+进行包私有。另请参阅 https://www.baeldung.com/maven-cant-find-junit-tests - banan3'14
9个回答

134
根据注释(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>

你使用的 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>

查看junit5-samples以获取此信息。

在Maven仓库中查看Maven Surefire Plugin构件。截至2019-01,版本为3.0.0-M3


5
我已添加依赖并运行了mvn clean test,但仍未运行任何测试。谢谢。 - mogoli
1
我已经将其删除,但仍然没有成功。我在Maven之外通过我的IDE运行了测试,它确实可以运行。谢谢。 - mogoli
5
提示:从JUnit 5版本5.4.0开始,您可以使用新的便捷单个Maven构件,名为JUnit Jupiter(聚合器),它会提供几个相关的构件,以编写和运行JUnit 5测试:junit-jupiter - Basil Bourque
刚刚偶然看到了这个答案。3.0.0-M3被认为是稳定的吗?我不确定版本号中的后缀,并且没有找到有关Maven插件版本方案的任何信息。 - Sebastian S
@SebastianS 官方页面声称,这是一个稳定版本:https://maven.apache.org/surefire/download.cgi - LaurentG
显示剩余5条评论

55

简述

在你的项目中添加两个依赖项:

  • JUnit Jupiter (聚合器)
  • Maven Surefire 插件
<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter -->
<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter</artifactId>
    <version>5.9.1</version>
    <scope>test</scope>
</dependency>

…和:

<!-- https://maven.apache.org/surefire/maven-surefire-plugin/dependency-info.html -->
<dependency>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-plugin</artifactId>
  <version>3.0.0-M7</version>
  <type>maven-plugin</type>
</dependency>

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

LaurentG的回答似乎是正确的,但有点过时。

从JUnit 5.4开始,您可以使用单个Maven构件替换这些多个Maven构件

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

... 使用一个构件:

…来运行JUnit 5测试。

这个构件是其他构件的聚合体,是一个方便的包装器来简化您的POM文件。请参见此存储库

<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter -->
<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter</artifactId>
    <version>5.9.1</version>
    <scope>test</scope>
</dependency>

这提供了你所需要的一切,以编写和运行JUnit 5 Jupiter测试。

maven-surefire-plugin

如同那个其他答案中所示,你还需要Surefire插件。请确保获取最新版本,因为Surefire最近进行了一些重要的修复/增强。

请查看适用于Maven、Buildr、Ivy、Grape、Grails、SBT和Leiningen的依赖关系声明列表。引用Maven配置:

<!-- https://maven.apache.org/surefire/maven-surefire-plugin/dependency-info.html -->
<dependency>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-plugin</artifactId>
  <version>3.0.0-M7</version>
  <type>maven-plugin</type>
</dependency>

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

如果您有旧的 JUnit 3 或 JUnit 4 遗留测试需要继续运行,请添加 junit-vintage-engine 依赖。

<!-- https://mvnrepository.com/artifact/org.junit.vintage/junit-vintage-engine -->
<dependency>
    <groupId>org.junit.vintage</groupId>
    <artifactId>junit-vintage-engine</artifactId>
    <version>5.9.1</version>
    <scope>test</scope>
</dependency>

2
为了运行传统的Junit 3/4测试,需要junit-vintage。我已经升级了surefire和junit,但我的测试没有被执行。引入junit-vintage依赖解决了这个问题。 - akila
添加surefire插件是主要的事情。仅有junit依赖项是不够的。 - foo
@foo 很好的建议。我加强了那一部分。 - Basil Bourque

12
  • 我遇到了类似的问题,其中 junit 的 @Test 注释可以使用,但 org.junit.jupiter.api.Test 的 @Test 注释不行。

解决方法:

  • 这里 是 Maven 官方关于如何配置 maven-surefire-plugin 并确保其使用正确的 junit-jupiter-engine 的指南。

  • 这份指南有点让人困惑,下面是我成功解决问题的方法:

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

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

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>


3
根据Junit 5文档,我们需要使用mvn test来执行测试:
<!-- ... -->
<dependencies>
    <!-- ... -->
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter</artifactId>
        <version>5.9.2</version>
        <scope>test</scope>
    </dependency>
    <!-- ... -->
</dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>3.0.0-M7</version>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-failsafe-plugin</artifactId>
            <version>3.0.0-M7</version>
        </plugin>
    </plugins>
</build>
<!-- ... -->

来源:


2

对于我来说(所有测试都使用Junit 5),在插件部分只需使用3.0.0-M7版本的surefire插件即可。无需将surefire插件添加到依赖项中,我只有org.junit.jupiter.junit-jupiter在我的依赖项中,没有其他jupiter依赖项。


这个对我有效。 - KnockingHeads

1
在Maven Surefire的配置部分添加“include”,你的测试文件应该以Test.java结尾,这样就可以正常运行。
<groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>3.0.0-M5</version>
            <configuration>
                <includes>
                    <include>**/*Test.java</include>
                </includes>
            </configuration>

谢谢,老兄!我为此苦恼了好久 :( 我简直不敢相信他们重新引入了那个古老的愚蠢的JUnit命名规则 :((( 真是太荒谬了! :((( - Rop
因此,为了避免这个问题,我们现在应该始终添加以下内容:<includes><include>**/*.java</include></includes> - Rop

1

最近我发现,为了让Maven执行我的测试,我必须在测试方法上添加前缀“test”。

@Test
public void testStringsAreEqual() {
    var aString = "Walrus Code";

    Assertions.assertEquals(aString, "Walrus Code");
}

否则测试将被忽略... 这是我的pom的一部分:
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.1</version>
            <configuration>
                <source>11</source>
                <target>11</target>
            </configuration>
        </plugin>

        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>3.0.0</version>
            <configuration>
                <mainClass>com.myapp.Main</mainClass>
            </configuration>
        </plugin>
    </plugins>
</build>

<dependencies>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter</artifactId>
        <version>5.7.1</version>
        <scope>test</scope>
    </dependency>
</dependencies>

0
在我的情况下,除了接受的答案中的两个更改(maven-surefire插件版本和org.junit.jupiter的构件)之外,我还需要添加一个更改。(使用的Java版本:11)
尽管org.jacoco.agent是我的依赖项列表的一部分,但它也需要在<dependencyManagement>标签中添加。这似乎已经解决了任何潜在的版本问题/自动强制依赖关系。
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.jacoco</groupId>
            <artifactId>org.jacoco.agent</artifactId>
            <version>${jacoco.version}</version>
            <classifier>runtime</classifier>
            <scope>test</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

0

对我来说,未运行测试是由于surefire插件根本没有运行。这是由于两个原因。

  1. 我在

    build->pluginmanagement->plugins

    中声明了sunfire插件,而不仅仅是

    build->plugins

  2. 我的类型是pom而不是jar。

我知道这些可能听起来像是故意犯的错误,但这是一个新手maven用户通过从不同项目复制粘贴创建pom.xml的结果。我也知道我的症状与作者描述的不同(插件根本没有运行而不是找不到任何测试),但我相信它很适合主题,并且这个解决方案可能会帮助一些人。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接