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

升级Eclipse后使用Gradle运行JUnit测试出现NoSuchMethodError

问题描述

此前使用Eclipse 2022-09版本时一切正常,所有依赖通过Gradle管理,JUnit 5版本由Spring Boot管理插件定义。升级至Eclipse 2023-09后,在Eclipse内执行JUnit测试时开始失败,报错如下:

java.lang.NoSuchMethodError: 'java.util.Set org.junit.platform.engine.TestDescriptor.getAncestors()'
at org.junit.platform.launcher.core.StackTracePruningEngineExecutionListener.getTestClassNames(StackTracePruningEngineExecutionListener.java:50)
at org.junit.platform.launcher.core.StackTracePruningEngineExecutionListener.executionFinished(StackTracePruningEngineExecutionListener.java:39)
at org.junit.platform.launcher.core.DelegatingEngineExecutionListener.executionFinished(DelegatingEngineExecutionListener.java:46)
at org.junit.platform.launcher.core.OutcomeDelayingEngineExecutionListener.reportEngineFailure(OutcomeDelayingEngineExecutionListener.java:83)
at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:203)
at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:169)
at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:93)
at org.junit.platform.launcher.core.EngineExecutionOrchestrator.lambda$execute$0(EngineExecutionOrchestrator.java:58)
at org.junit.platform.launcher.core.EngineExecutionOrchestrator.withInterceptedStreams(EngineExecutionOrchestrator.java:141)
at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:57)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:103)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:94)
at org.junit.platform.launcher.core.DelegatingLauncher.execute(DelegatingLauncher.java:52)
at org.junit.platform.launcher.core.SessionPerRequestLauncher.execute(SessionPerRequestLauncher.java:70)
at org.eclipse.jdt.internal.junit5.runner.JUnit5TestReference.run(JUnit5TestReference.java:98)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:40)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:529)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:756)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:452)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:210)

使用Adoptium OpenJDK 17(17.0.8.101-hotspot),通过gradlew test --tests Xyz命令执行测试一切正常,仅在Eclipse中运行测试时出现问题。

原因分析

这个错误是JUnit Platform组件版本不兼容导致的:

  • Eclipse 2023-09内置的JUnit Platform Launcher版本(通常为1.10.x或更高)调用了TestDescriptor.getAncestors()方法,该方法是JUnit Platform Engine 1.9.0及以上版本才新增的。
  • 项目通过Spring Boot插件管理的JUnit Platform Engine版本可能低于1.9.0,Eclipse运行测试时混合使用了内置高版本Launcher和项目低版本Engine,导致方法找不到。
  • Gradle命令行测试正常是因为它会使用项目依赖中统一的版本,不会混入Eclipse内置组件。
解决方案

1. 显式统一JUnit Platform版本(推荐)

在build.gradle(或build.gradle.kts)中,显式指定JUnit Platform的版本,和Spring Boot插件管理的版本保持一致,确保Launcher和Engine版本匹配:

// build.gradle
ext {
    junitPlatformVersion = springBootPlugin.ext['junit-platform.version']
}

dependencies {
    // 确保所有JUnit Platform组件版本统一
    testImplementation platform("org.junit:junit-platform:${junitPlatformVersion}")
    testImplementation 'org.junit.platform:junit-platform-engine'
    testImplementation 'org.junit.platform:junit-platform-launcher'
    // 其他JUnit 5依赖...
}

如果是Kotlin DSL:

// build.gradle.kts
val junitPlatformVersion by extra(springBootPlugin.extra["junit-platform.version"] as String)

dependencies {
    testImplementation(platform("org.junit:junit-platform:$junitPlatformVersion"))
    testImplementation("org.junit.platform:junit-platform-engine")
    testImplementation("org.junit.platform:junit-platform-launcher")
    // 其他JUnit 5依赖...
}

2. 配置Eclipse使用项目依赖而非内置组件

在Eclipse中调整测试运行配置:

  • 右键点击测试类/方法 → Run As → Run Configurations...
  • 选择对应的JUnit 5配置,切换到Classpath标签页
  • 移除Eclipse JUnit Launcher相关的类库,添加项目依赖中的junit-platform-launcher jar包
  • 点击Apply后重新运行测试

3. 更新Spring Boot版本(如果可行)

如果你的Spring Boot版本较旧,升级到支持更高JUnit Platform版本的Spring Boot版本(比如Spring Boot 2.7.x+对应JUnit Platform 1.9.x,Spring Boot 3.x对应JUnit Platform 1.10.x+),这样Spring Boot管理的JUnit版本会自动适配Eclipse的内置组件。

4. 清理Eclipse缓存和项目配置

  • 执行Project → Clean...,清理项目并重新构建
  • 删除项目根目录下的.gradle和.settings文件夹,以及bin/build目录
  • 重新导入项目:File → Import → Existing Gradle Project

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.10 06:41:33