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

在STS 4.20.0中运行JUnit测试时出现NoSuchMethodError问题求助

STS中JUnit测试执行正常但结束时抛出NoSuchMethodError异常

问题现象

在Spring Tool Suite (4.20.0)中右键执行JUnit测试时,测试逻辑正常执行(日志输出Transforming XML),但测试结束后抛出如下错误:

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)

注意:使用Gradle任务在STS外运行测试时一切正常。

项目配置

Gradle插件配置

plugins {
    id 'java'
    id 'eclipse'
    id 'eclipse-wtp'
    id 'org.springframework.boot' version "3.1.4"
    id 'io.spring.dependency-management' version "1.1.3"
}

依赖配置

implementation("org.springframework.boot:spring-boot-starter-parent:3.1.4")
implementation("org.springframework.boot:spring-boot-starter-web")
implementation("org.springframework.boot:spring-boot-configuration-processor")
implementation("org.springframework.boot:spring-boot-starter-test")

代码示例

测试类

@ExtendWith(SpringExtension.class)
@ContextConfiguration(initializers = ConfigDataApplicationContextInitializer.class, classes = { TransformService.class})
@Slf4j
public class TransformServiceTest {
    @Autowired
    TransformService transformService;
    @Test
    public void transformXmlTest() throws Exception {
        transformService.transformXml();
    }
}

服务类

@Service
@Slf4j
public class TransformService {
    public void transformXml() {
        log.info("Transforming XML");
    }
}

完整运行日志

OpenJDK 64-Bit Server VM warning: Sharing is only supported for boot loader classes because bootstrap classpath has been appended
2023-10-06 10:00:33.097  INFO Transforming XML | com.dowill.xsl.service.TransformService
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)

问题分析与解决方案

核心原因

该异常由JUnit Platform版本不兼容导致:

  • STS内置的JUnit Launcher组件依赖的org.junit.platform:junit-platform-launcher版本,与Spring Boot 3.1.4引入的JUnit Platform核心库版本不一致。
  • TestDescriptor.getAncestors()方法是JUnit Platform 1.8+版本新增的,如果STS使用的Launcher版本低于此版本,而项目依赖的核心库为高版本,就会出现方法找不到的错误。

解决步骤

  1. 强制统一JUnit Platform版本:在Gradle的dependencyManagement块中显式指定JUnit Platform相关依赖版本,确保与Spring Boot依赖版本一致:
dependencyManagement {
    imports {
        mavenBom org.springframework.boot.gradle.plugin.SpringBootPlugin.BOM_COORDINATES
    }
    dependencies {
        dependency 'org.junit.platform:junit-platform-launcher:1.10.0' // 对应Spring Boot 3.1.4的版本
    }
}
  1. 更新STS的JUnit插件:通过STS的Help > Eclipse Marketplace搜索JUnit相关插件并更新至最新版本,确保内置Launcher组件版本与项目依赖匹配。

  2. 使用Gradle任务在STS内运行测试:暂时可通过STS的Gradle视图执行test任务运行测试,规避STS内置JUnit launcher的版本冲突问题。

  3. 修复依赖配置错误:原依赖中错误地将spring-boot-starter-parent作为implementation依赖,它应在build.gradle的parent块中声明,而非普通依赖:

plugins {
    id 'java'
    id 'eclipse'
    id 'eclipse-wtp'
    id 'org.springframework.boot' version "3.1.4"
    id 'io.spring.dependency-management' version "1.1.3"
}

parent {
    id 'org.springframework.boot'
    version '3.1.4'
}

该错误虽不直接引发当前异常,但会造成依赖管理混乱,建议修正。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.09 13:27:04