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

运行测试类时出现java.lang.NoClassDefFoundError报错,如何解决?

解决java.lang.NoClassDefFoundError: org/junit/platform/launcher/TestIdentifier异常

问题原因

这个异常是因为项目运行测试时找不到org.junit.platform.launcher.TestIdentifier类,核心原因是缺少JUnit Platform Launcher相关依赖,或者依赖配置错误导致运行时无法加载该类。

解决办法

Maven项目配置

在pom.xml里添加以下依赖:

<dependency>
    <groupId>org.junit.platform</groupId>
    <artifactId>junit-platform-launcher</artifactId>
    <version>1.10.0</version> <!-- 建议使用最新稳定版本 -->
    <scope>test</scope>
</dependency>

添加后执行mvn clean install刷新依赖,也可以在IDE中手动同步Maven项目。

Gradle项目配置

在build.gradle中加入依赖:

testImplementation 'org.junit.platform:junit-platform-launcher:1.10.0'

执行gradle build --refresh-dependencies更新依赖。

排查依赖范围

如果已经添加依赖但仍报错,检查依赖范围:Maven中的scope若为test,仅在测试代码中生效;如果你的测试逻辑需要在运行时调用该类,可将scope改为compile或runtime,不过常规测试场景下test范围足够。

版本兼容性检查

确保JUnit Platform Launcher的版本与你使用的JUnit 5(Jupiter)版本一致,比如JUnit Jupiter用5.10.0,Launcher也对应使用1.10.0,避免版本不兼容引发问题。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.18 18:52:05