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

为何Maven无法找到后缀为Test的测试文件?求排查

问题排查与解决方案

1. JUnit Jupiter依赖版本不兼容

你的junit-jupiter-api版本是5.9.1,但junit-jupiter-engine版本是5.4.0,两者版本差异过大,会导致测试引擎无法识别API定义的测试注解,直接造成测试用例无法被检测到。

解决方法:统一两者版本,建议使用匹配的稳定版,同时给junit-jupiter-engine加上<scope>test</scope>(测试依赖无需打包到生产环境):

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

2. Maven Surefire插件版本过低

你使用的maven-surefire-plugin是2.22.1,这个版本对JUnit 5的支持需要额外配置,否则无法自动识别JUnit Jupiter的测试类。

解决方法:升级插件到支持JUnit 5的版本(比如3.1.2),新版本会自动适配JUnit Jupiter,无需额外配置:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>3.1.2</version>
</plugin>

3. 测试类/方法不符合识别规则

检查以下细节:

  • 测试类命名是否符合默认规则:类名以Test结尾(比如UserServiceTest),或者以Tests结尾
  • 测试方法是否添加了JUnit Jupiter的@Test注解(注意是org.junit.jupiter.api.Test,不是JUnit 4的org.junit.Test)
  • 测试类的包声明是否和文件路径一致:比如文件在src/test/java/com/example/myapp/testingit下,类的包必须是package com.example.myapp.testingit;

4. 额外排查手段

如果以上调整后仍有问题,可以执行mvn test -X查看详细日志,定位具体的扫描失败原因,比如是否存在编译错误、测试类未被编译等。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.07 14:45:24