IntelliJ运行JUnit测试提示“No test were found”问题求助
解决JUnit测试「No test were found」的问题
从错误栈里的NoClassDefFoundError: org/junit/jupiter/api/io/TempDirFactory可以直接定位问题:你的项目缺少JUnit Jupiter的核心依赖,或者依赖版本不兼容。这个类属于JUnit 5的API模块,找不到它会导致测试引擎无法正常发现和运行测试。
修复步骤:
1. 检查并修正项目依赖
根据你用的构建工具,确保引入了正确的JUnit Jupiter依赖,且版本统一:
如果你用Maven(pom.xml):
添加以下依赖,版本建议用最新稳定版(比如5.9.2):
<dependencies> <!-- JUnit Jupiter API,用于编写测试 --> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <version>5.9.2</version> <scope>test</scope> </dependency> <!-- JUnit Jupiter Engine,负责执行测试,必须引入 --> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-engine</artifactId> <version>5.9.2</version> <scope>test</scope> </dependency> </dependencies>
如果你用Gradle(build.gradle):
dependencies { testImplementation 'org.junit.jupiter:junit-jupiter-api:5.9.2' testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.9.2' } // 启用JUnit Platform运行测试 test { useJUnitPlatform() }
2. 刷新项目依赖
- Maven:点击IDE里的「Reload All Maven Projects」按钮,确保依赖下载完成。
- Gradle:点击「Refresh Gradle Project」按钮,同步依赖。
3. 检查IDE配置
- 确保测试类放在
src/test/java目录下,IDE默认只会识别这个目录下的类为测试类。 - 检查IDE的JUnit设置:在IntelliJ中,进入
File > Project Structure > Libraries,确认JUnit的库版本和你配置的一致,没有冲突的旧版本。
你的测试代码本身没有问题,@Test注解是正确的JUnit 5用法,只要依赖配置正确,测试就能正常运行。
内容的提问来源于stack exchange,提问作者Rookiw
相关产品推荐
相关产品推荐

