【问题标题】:running test suite is not working in junit5 through maven运行测试套件无法通过 maven 在 junit5 中运行
【发布时间】:2025-12-01 19:40:01
【问题描述】:

@SelectPackages 和 @SelectClasses 标签没有被 maven 测试命令解析。虽然它在 IDE 中运行良好。即使我尝试在 pom.xml 中使用标签。

这里是代码sn-p:


PaymentServiceTest.java

package xyz.howtoprogram.junit5.payment;

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

import org.junit.jupiter.api.Test;
import org.junit.platform.runner.JUnitPlatform;
import org.junit.runner.RunWith;

@RunWith(JUnitPlatform.class)
public class PaymentServiceTest {
  @Test
  public void doPaymentZeroAmount() {
    assertEquals(1, 1);
  }
}

UserFeatureSuiteTest.java

@RunWith(JUnitPlatform.class)
@SelectPackages("xyz.howtoprogram.junit5.payment")
public class UserFeatureSuiteTest {
}

它没有运行包下的任何测试用例。虽然它下面有一个测试用例。

xyz.howtoprogram.junit5.payment -> PaymentServiceTest.java

Running xyz.howtoprogram.junit5.suite.UserFeatureSuiteTest
Tests run: 0, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0 sec - in xyz.howtoprogram.junit5.suite.UserFeatureSuiteTest.

即使我尝试更改 pom.xml,例如添加“包含”标签。


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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<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.0.0-M2</junit.jupiter.version>
    <junit.vintage.version>4.12.0-M2</junit.vintage.version>
    <junit.platform.version>1.0.0-M2</junit.platform.version>
</properties>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.19.1</version>
            <configuration>
                <includes>
                    <include>**/UserFeatureSuiteTest.java</include>
                </includes>
            </configuration>
            <dependencies>
                <dependency>
                    <groupId>org.junit.jupiter</groupId>
                    <artifactId>junit-jupiter-engine</artifactId>
                    <version>${junit.jupiter.version}</version>
                </dependency>
                <dependency>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                    <version>${junit.vintage.version}</version>
                </dependency>
            </dependencies>
        </plugin>
    </plugins>
</build>

<dependencies>



    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>org.junit.platform</groupId>
        <artifactId>junit-platform-runner</artifactId>
    </dependency>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-engine</artifactId>
    </dependency>
    <dependency>
        <groupId>org.junit.vintage</groupId>
        <artifactId>junit-vintage-engine</artifactId>
    </dependency>
</dependencies>

【问题讨论】:

    标签: junit5


    【解决方案1】:

    默认情况下,Maven 在查找要运行的测试时使用以下命名约定:

    • Test*
    • *Test
    • *TestCase

    您的测试类不遵循这些约定。您应该重命名它或 configure Maven Surefire Plugin 以使用另一种模式来测试类。

    可能导致 Maven 无法工作的另一件事是所有测试都应该位于以下文件夹中:

    /my_application/src/test/java/MyFirstExampleTest.java

    Here 你可以看到一个很好的问题来概括你的问题,我从那里得到了我的部分答案。你应该看看它。

    编辑

    在这里你可以看到一个例子来解释你的 pom.xml 应该是怎样的:

    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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>org.codefx.demo</groupId>
        <artifactId>junit-5</artifactId>
        <version>1.0-SNAPSHOT</version>
    
        <dependencies>
            <dependency>
                <groupId>org.junit.jupiter</groupId>
                <artifactId>junit-jupiter-api</artifactId>
                <version>5.0.0-M3</version>
                <scope>test</scope>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.1</version>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                        <compilerArgs>
                            <arg>-parameters</arg>
                        </compilerArgs>
                    </configuration>
                </plugin>
                <plugin>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.19</version>
                    <dependencies>
                        <dependency>
                            <groupId>org.junit.platform</groupId>
                            <artifactId>junit-platform-surefire-provider</artifactId>
                            <version>1.0.0-M3</version>
                        </dependency>
                        <dependency>
                            <!-- contains the engine that actually runs the Jupiter-tests -->
                            <groupId>org.junit.jupiter</groupId>
                            <artifactId>junit-jupiter-engine</artifactId>
                            <version>5.0.0-M3</version>
                        </dependency>
                    </dependencies>
                </plugin>
            </plugins>
        </build>
    
    </project>
    

    正如您在此配置文件中看到的,您指定:

    • 依赖关系:您需要 JUnit 的测试范围依赖关系才能运行测试
    • 在构建部分,您将添加运行测试及其依赖项的 surefire 插件

    【讨论】:

    • 它没有帮助。所以,我用详细的步骤重新编辑了原始问题。
    • @SankarsanPadhy 编辑了我的答案,向您解释了您的 pom 应该如何。我认为这可能是你的问题
    • 不工作。我用完整的 pom.xml 重新编辑了原始问题
    • @SankarsanPadhy 您是否尝试将测试类的名称从 TestPaymentService.java 重构为 PaymentServiceTest.java。我正在调查junit.org/junit5/docs/current/user-guide,我认为,西装只会识别与此匹配的类 ^.*Tests?$
    • 我重构了,没有更新原问题。它不工作。我强烈要求你自己运行一个小程序。我尝试了几次试验和错误,但直到现在都没有成功。
    【解决方案2】:

    @RunWith(JUnitPlatform.class) 在通过 JUnit 平台运行测试时不受支持。如果新的提供者同时接收UserFeatureSuiteTestPaymentServiceTest,您的测试将被执行两次。

    来自User Guide

    使用 @RunWith(JUnitPlatform.class) 注释类允许它与 IDE 一起运行,并构建支持 JUnit 4 但尚不直接支持 JUnit 平台的系统。

    因此,如果您希望 Maven Surefire 运行 UserFeatureSuiteTest,您可以使用默认检测到的“旧”JUnit 4 支持,即只需从插件的依赖项中删除 junit-platform-surefire-provider

    或者,您可以直接执行测试,例如PaymentServiceTest,通过 JUnit 平台使用您现有的配置。

    【讨论】:

    • 不,很遗憾,即使删除了surefire-provider,它也无法正常工作。
    • 同时进行 Jupiter 和 Vintage 测试的项目怎么样?
    • 不,@RunWith(JUnitPlatform.class) 只能用于 JUnit 4。
    【解决方案3】:

    很抱歉给您带来了困惑...我只是仔细查看了您的代码并意识到您正在尝试混合两个概念。

    您的测试代码使用 JUnit 5 @Test 进行注释。因此,此代码必须使用 junit-jupiter-engine 运行。该引擎确实支持声明性套件,但确实从 maven-surefire-plugin 读取配置。

    @RunWith 是一个 JUnit4 注释,在 junit-jupiter-engine 中被完全忽略。我认为它不会导致测试失败,但它也不会启用任何 JUnit 5 测试。在这种情况下,您放置在套件类上的 @SelectPackages 注释只会帮助确定运行哪些 JUnit 4 测试 - 但您没有任何测试。

    【讨论】:

    • 我刚刚重新编辑了原来的pom.xml。我已经为vintage和jupiter(其中之一或两者)添加了surefire依赖;它并非在所有情况下都有效。你可以试试一个简单的程序。
    • @SankarsanPadhy - 不,我绝对不会帮助您为像 how-to-program.xyz 这样的“答案网站”构建教程。如果您想提供专家建议,则需要花时间成为专家。
    • 很抱歉给您带来了困惑。我正在尝试在我们的项目中实现 junit5 功能。因此,我四处寻找文档和示例站点来试用它们;但偶然发现了这个问题。于是,拼命寻求帮助。我不能只在这里发布我的公司代码。所以发布了我在本地系统中尝试的示例。
    • @SankarsanPadhy - 我很抱歉如此挑剔......我根据您上面的示例做了一些错误的假设,不应该立即驳回您的问题。跨度>
    • 没有问题@steve-moyer。我试过了,只是想出了解决方案。请参阅我的最新帖子。
    【解决方案4】:

    下面是最终的 POM,它现在可以工作了。

    <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.howtoprogram</groupId>
    <artifactId>junit5-test-suite-example</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    
    
    <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.0.0-M2</junit.jupiter.version>
        <junit.vintage.version>4.12.0-M2</junit.vintage.version>
        <junit.platform.version>1.0.0-M2</junit.platform.version>
    </properties>
    
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.19</version>
                <configuration>
                    <includes>
                        <include>**/*SuiteTest.java</include>
                    </includes>
                </configuration>
    
            </plugin>
        </plugins>
    </build>
    
    <dependencies>
        <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-runner</artifactId>
            <version>${junit.platform.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>
    </dependencies>
    

    所以输出是:

         T E S T S
    -------------------------------------------------------
    Feb 23, 2017 10:21:06 PM org.junit.platform.launcher.core.ServiceLoaderTestEngineRegistry loadTestEngines
    INFO: Discovered TestEngines with IDs: [junit-jupiter]
    Running xyz.howtoprogram.junit5.suite.UserFeatureSuiteTest
    Feb 23, 2017 10:21:06 PM org.junit.platform.launcher.core.ServiceLoaderTestEngineRegistry loadTestEngines
    INFO: Discovered TestEngines with IDs: [junit-jupiter]
    Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.053 sec - in xyz.howtoprogram.junit5.suite.UserFeatureSuiteTest
    

    【讨论】:

    • 插件依赖项似乎存在问题,因为删除它们也对我有帮助。