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

运行JUnit5-jupiter-starter-maven项目时出现NoClassDefFoundError错误求助

解决JUnit 5 r5.2.0示例java.lang.NoClassDefFoundError问题

这个问题我之前排查过,核心原因是你使用的maven-surefire-plugin版本和JUnit 5.2.0不兼容,旧版插件无法正确加载JUnit平台的启动器依赖,才抛出了NoClassDefFoundError: org/junit/platform/launcher/core/LauncherFactory错误。下面是具体的解决步骤:

1. 更新Maven Surefire插件版本

你的示例里用的是2.21.0版本的Surefire插件,这个版本对JUnit 5的支持不完善。打开项目根目录的pom.xml,找到插件配置块,把版本升级到2.22.0及以上(推荐用稳定兼容的2.22.2):

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

2. 补充JUnit Platform Launcher依赖(可选,若升级插件后仍报错)

如果更新插件后问题依旧,需要显式添加JUnit平台启动器的依赖到pom.xml的dependencies节点中,版本要和JUnit 5.2.0匹配(对应1.2.0):

<dependency>
    <groupId>org.junit.platform</groupId>
    <artifactId>junit-platform-launcher</artifactId>
    <version>1.2.0</version>
    <scope>test</scope>
</dependency>

3. 重新执行测试

保存pom.xml的修改后,再次运行清理和测试命令:

mvnw clean test

这样应该就能正常执行JUnit 5的测试用例了——本质就是让Surefire插件和JUnit 5的依赖版本对齐,确保测试启动器能被正确加载。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.29 09:06:51