You need to enable JavaScript to run this app. Gradle运行JUnit5+Mockito测试报ClassNamePatternFilterUtils类未找到如何解决
优惠活动
大模型
产品
解决方案
定价
更多

Gradle运行JUnit5+Mockito测试报ClassNamePatternFilterUtils类未找到如何解决

错误原因

这个java.lang.NoClassDefFoundError报错的核心原因是JUnit Platform相关组件版本不匹配:

  • org.junit.platform.commons.util.ClassNamePatternFilterUtils是JUnit Platform Commons 1.7.0及以上版本才新增的类
  • 你项目中使用的junit-platform-launcher版本≥1.7.0,调用了这个新增类,但实际依赖引入的junit-platform-commons版本低于1.7.0,运行时就找不到对应的类。
    从报错栈可以看到你是用Eclipse JDT的JUnit5运行器执行测试,也可能是IDE自带的JUnit运行器版本和项目依赖的JUnit版本不兼容导致。
排查步骤
  • 执行Gradle命令查看测试依赖的版本冲突:
gradle dependencies --configuration testRuntimeClasspath | grep "junit-platform"

对比输出中junit-platform-launcher和junit-platform-commons的版本号,确认两个组件版本是否一致,是否存在低版本的junit-platform-commons被其他依赖强制引入的情况。

解决方案
  1. 统一JUnit5版本管理
    在dependencies块中引入JUnit官方的BOM来统一所有组件版本,避免冲突:
dependencies {
    // 新增这行,放在test相关依赖最前面即可,版本可以根据你的Spring Boot版本适配,5.7.0以上即可
    testImplementation platform('org.junit:junit-bom:5.7.2')
    // 你原来的其他依赖保持不变
}
  1. 适配IDE运行配置
    如果是Eclipse环境触发的报错,打开测试的运行配置,勾选「Use project's JUnit5 version」选项,避免使用IDE自带的旧版本JUnit运行器。
  2. 清理缓存重新构建
    执行以下命令刷新依赖后重新运行测试:
gradle clean build --refresh-dependencies

内容的提问来源于stack exchange,提问作者Sri Santhosh Natarajan

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.08 19:42:00