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

Eclipse 2023-09中JUnit 5.9.x测试报错NoSuchMethodError,求助解决

解决Spring Boot默认测试启动报错java.lang.NoSuchMethodError: org.junit.platform.engine.TestDescriptor.getAncestors()

问题分析

这个报错的核心原因是JUnit Platform与JUnit Jupiter版本不兼容:getAncestors()方法是JUnit Platform 1.9.x及以上版本新增的API,若项目中JUnit Platform版本低于该要求,而JUnit Jupiter使用了更高版本,就会出现方法找不到的错误。

你手动添加的junit-jupiter-api和junit-jupiter-engine版本(5.9.3)对应的JUnit Platform版本应为1.9.3,但Spring Boot 3.1.3的spring-boot-starter-test已经通过依赖管理锁定了一套兼容的JUnit版本,手动指定版本会打破这种兼容性,导致依赖冲突。另外,Eclipse中手动添加的JUnit库也可能和项目依赖版本不一致,加剧冲突。

解决方案

1. 恢复Spring Boot默认的JUnit依赖管理

移除build.gradle中手动添加的JUnit Jupiter依赖,让spring-boot-starter-test自动管理所有测试依赖的版本:

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    developmentOnly 'org.springframework.boot:spring-boot-devtools'
    // 仅保留starter-test,它已包含所有必要的JUnit依赖
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

2. 清理项目依赖缓存并重新构建

执行Gradle命令清理缓存并重新同步依赖:

./gradlew clean build --refresh-dependencies

在Eclipse中,也可以通过Gradle -> Refresh Gradle Project重新同步项目。

3. 确保Eclipse的JUnit插件版本与项目依赖匹配

  • 打开Eclipse的Help -> About Eclipse IDE -> Installation Details,找到JUnit相关插件,确认其版本(Spring Boot 3.1.3对应的JUnit Jupiter版本为5.9.2,JUnit插件版本需与之匹配)。
  • 若版本不匹配,通过Help -> Eclipse Marketplace搜索并更新JUnit插件到对应版本。
  • 移除Eclipse中手动添加到Build Path的JUnit库,让Gradle自动管理项目类路径。

4. 验证测试启动

完成上述步骤后,重新运行默认的Spring Boot测试类,问题即可解决。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.11 01:42:36