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

Jenkins构建Gradle项目Test任务失败:NoSuchMethodError问题求助

问题诊断与解决方案

核心问题分析

你遇到的java.lang.NoSuchMethodError属于依赖版本冲突,加上测试类配置错误,共同导致测试任务失败:

  1. 手动指定的JUnit Jupiter 5.8.2版本与Spring Boot 3.2.5内置的JUnit版本不兼容,Spring Extension在调用不存在的方法时抛出错误。
  2. 测试类中@SpringBootTest指定了错误的启动类(测试类本身而非主应用类),导致Spring上下文初始化异常。

分步修复

1. 修正测试类配置

修改ArtGalleryApplicationTests.java:

  • 移除冗余的@ExtendWith(SpringExtension.class)(Spring Boot 2.2+后@SpringBootTest已默认集成该扩展)
  • 将@SpringBootTest的classes参数改为你的主应用类(比如ArtGalleryApplication.class,而非测试类)

修改后的测试类:

package com.example.art_gallery;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest(classes = ArtGalleryApplication.class)
class ArtGalleryApplicationTests {

    @Test
    void contextLoads() {
    }

}

2. 修复依赖版本冲突

在build.gradle中,删除手动指定的JUnit Jupiter版本,直接使用Spring Boot依赖管理提供的兼容版本:

  • 移除testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.2'和testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.2'
  • 保留spring-boot-starter-test即可,它已包含所有必要的JUnit 5依赖

修改后的测试依赖块:

// Test
testImplementation('org.springframework.boot:spring-boot-starter-test') {
    exclude module: 'junit-vintage-engine' // 仅排除旧版JUnit 4引擎,无需排除junit模块
}
testImplementation "org.springframework.security:spring-security-test"

// JUnit Platform 保留启动器即可,无需手动指定版本
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'

3. 可选:移除手动指定的Hibernate版本

Spring Boot的spring-boot-starter-data-jpa已经集成了兼容的Hibernate版本,手动指定org.hibernate:hibernate-core:6.5.2.Final可能导致版本冲突,建议删除这一行依赖,让Spring Boot自动管理。


验证修复

在Jenkins重新构建前,先在本地执行测试命令验证:

./gradlew test

如果本地测试通过,再推送代码到Jenkins执行构建。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.23 03:27:38