You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Maven可运行JUnit 5@Test测试,但无法执行@TestFactory方法求助

问题描述

项目中多个使用org.junit.jupiter.api.Test注解的JUnit 5测试,可通过mvn test命令正常运行。但包含org.junit.jupiter.api.TestFactory注解方法的测试类,在IDEA中能正常执行所有测试(包括该TestFactory生成的动态测试);通过Maven运行时,虽显示正在执行该类,却未运行其中任何方法,指定该测试类执行时显示“Tests run: 0”。

尝试添加空的@Test注解方法,发现仅当方法名以‘test’开头时Maven才会执行,而@TestFactory方法即便修改名称也无法运行。

相关代码示例

import org.junit.jupiter.api.TestFactory;

// 类定义等内容...

public class YourTestClass {
    @TestFactory
    public Collection<DynamicTest> runTests() throws Exception {
        System.out.println("[Test] TestFactory test running");

        // 其他代码...
    }
}

POM文件原JUnit相关配置

<properties>
    <junit-platform.version>5.9.1</junit-platform.version>
</properties>

<!-- 原依赖部分 -->
<dependencies>
    <dependency>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>3.0.0-M7</version>
        <type>maven-plugin</type>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-engine</artifactId>
        <version>5.9.1</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-params</artifactId>
        <version>5.9.1</version>
        <scope>test</scope>
    </dependency>
</dependencies>

mvn -v输出

Apache Maven 3.8.5 (3599d3414f046de2324203b78ddcf9b5e4388aa0)
Maven home: /usr/local/Cellar/maven/3.8.5/libexec
Java version: 1.8.0_161, vendor: Oracle Corporation, runtime: /Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/jre
Default locale: en_GB, platform encoding: UTF-8
OS name: "mac os x", version: "10.16", arch: "x86_64", family: "mac"

解决方法
  • 修正Surefire插件配置方式:你当前把Surefire插件作为依赖添加是错误的,正确做法是在<build><plugins>节点下配置插件,同时移除依赖中的Surefire条目:
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>3.0.0-M7</version>
            <!-- 明确绑定JUnit 5引擎 -->
            <dependencies>
                <dependency>
                    <groupId>org.junit.jupiter</groupId>
                    <artifactId>junit-jupiter-engine</artifactId>
                    <version>5.9.1</version>
                </dependency>
            </dependencies>
        </plugin>
    </plugins>
</build>
  • 确保Java版本兼容性:JUnit Jupiter 5.9.1兼容Java 8,你当前使用的Java 1.8.0_161符合要求。如果后续仍有问题,可尝试升级Surefire插件到稳定版(如3.2.5),并保持JUnit版本与插件版本匹配。

  • 检查测试类/方法可见性:@TestFactory方法必须是public(你的代码已满足),同时测试类建议设为public,避免包私有访问权限导致Maven无法识别。

  • 强制使用JUnit 5规则:上述插件配置通过绑定JUnit 5引擎,会让Maven完全遵循JUnit 5的注解规则,不再沿用JUnit 4的类名/方法名前缀要求(比如不需要方法名以test开头)。

修改配置后,执行mvn clean test即可正常运行@TestFactory生成的动态测试。


内容的提问来源于stack exchange,提问作者AntonyM

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.31 05:24:20