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

运行mvn test识别到测试类但Tests run为0的问题排查求助

问题原因分析

你遇到的测试用例识别失败核心原因是版本不兼容:

  • 你当前使用的maven-surefire-plugin 2.19.1版本发布时间早于JUnit 5(JUnit Jupiter),无法识别org.junit.jupiter.api包下的@Test、@ParameterizedTest、@BeforeEach等注解,因此虽然能扫描到符合命名规则的测试类,但是找不到可执行的测试方法,最终返回0用例执行的结果。
  • 附加检查项:你的测试类、测试方法均为public修饰,命名也符合你配置的**/*Test.java规则,不存在其他基础配置错误。
解决方案

按以下两步修改pom.xml配置即可解决:

  1. 升级maven-surefire-plugin到支持JUnit 5的稳定版本,示例配置如下:
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>3.2.2</version>
    <configuration>
        <includes>
            <include>**/*Test.java</include>
        </includes>
    </configuration>
</plugin>
  1. 确保测试依赖中添加了JUnit 5对应的执行引擎、参数化测试依赖,示例配置如下:
<dependencies>
    <!-- JUnit 5核心API -->
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-api</artifactId>
        <version>5.10.0</version>
        <scope>test</scope>
    </dependency>
    <!-- JUnit 5执行引擎,surefire通过该引擎执行测试用例 -->
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-engine</artifactId>
        <version>5.10.0</version>
        <scope>test</scope>
    </dependency>
    <!-- 支持@ParameterizedTest等参数化测试注解 -->
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-params</artifactId>
        <version>5.10.0</version>
        <scope>test</scope>
    </dependency>
    <!-- 其余原有依赖保持不变 -->
</dependencies>

修改完成后执行mvn clean test即可正常识别并执行所有测试用例。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.08 23:00:03