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

使用@WithMockUser测试时出现NoSuchMethodError错误求助

问题:Spring Security测试抛出NoSuchMethodError

我运行以下Kotlin测试代码:

@Test
@WithMockUser(username = "myUser", roles = ["USER"])
fun givenExistingFirstnames_whenGetRequest_thenReturnsFirstnameJsonWithStatus200() {
    every { firstnameService.findFirstnames() } returns firstnames

    mockMvc.perform(get("/api/v1/firstnames"))
        .andExpect(status().isOk)
        .andExpect(content().contentType(MediaType.APPLICATION_JSON))
        .andExpect(jsonPath("$.firstname").value("Amadou"))
}

执行时报错:

java.lang.NoSuchMethodError: 'org.springframework.security.authentication.UsernamePasswordAuthenticationToken org.springframework.security.authentication.UsernamePasswordAuthenticationToken.authenticated(java.lang.Object, java.lang.Object, java.util.Collection)'          

我的Gradle依赖配置如下:

dependencies {
    implementation("org.springframework.boot:spring-boot-starter-data-jpa:2.7.3")
    implementation("org.springframework.boot:spring-boot-starter-data-rest:2.7.3")
    implementation("org.postgresql:postgresql:42.4.2")
    implementation("org.springframework.boot:spring-boot-starter-security:2.7.3")
    implementation("org.springframework.boot:spring-boot-starter-web:2.7.3")
    implementation("com.fasterxml.jackson.module:jackson-module-kotlin:2.13.3")
    implementation("org.jetbrains.kotlin:kotlin-reflect:1.6.0")
    implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.6.0")
    implementation("com.sipios:spring-search:0.2.4")
    implementation("org.junit.jupiter:junit-jupiter:5.9.0")
    // https://mvnrepository.com/artifact/org.springdoc/springdoc-openapi-ui
    implementation("org.springdoc:springdoc-openapi-ui:1.6.10")
    // https://mvnrepository.com/artifact/com.opencsv/opencsv
    implementation("com.opencsv:opencsv:5.6")
    // https://mvnrepository.com/artifact/io.jsonwebtoken/jjwt
    implementation("io.jsonwebtoken:jjwt:0.9.1")
    // https://mvnrepository.com/artifact/org.liquibase/liquibase-core
    //implementation("org.liquibase:liquibase-core:4.7.1")
    runtimeOnly("com.h2database:h2:2.1.214")
    testImplementation("org.springframework.boot:spring-boot-starter-test:2.7.3") {
        exclude(group = "org.mockito", module = "mockito-core")
    }
    testImplementation("org.springframework.security:spring-security-test:5.7.3")
    testImplementation("com.ninja-squad:springmockk:3.1.1")
}

看起来所有依赖版本都是对齐的,但还是出现了这个错误。


解决方案

这个错误的核心原因是类路径中存在版本不一致的Spring Security核心库:UsernamePasswordAuthenticationToken.authenticated()方法是在Spring Security 5.2+版本中新增的,如果类路径中混入了更低版本的spring-security-core,就会触发这个找不到方法的错误。

步骤1:检查依赖冲突

先执行Gradle命令查看spring-security-core的依赖树,确认是否有其他依赖间接引入了旧版本:

./gradlew dependencyInsight --dependency org.springframework.security:spring-security-core

输出结果里如果看到多个版本的spring-security-core,就说明存在冲突。

步骤2:强制统一Spring Security版本

在Gradle配置中添加版本约束,强制所有Spring Security相关依赖使用与Spring Boot 2.7.3匹配的5.7.3版本:

configurations.all {
    resolutionStrategy {
        force("org.springframework.security:spring-security-core:5.7.3")
        force("org.springframework.security:spring-security-web:5.7.3")
        force("org.springframework.security:spring-security-config:5.7.3")
    }
}

步骤3:优化测试依赖

spring-boot-starter-test已经包含了适配当前Boot版本的spring-security-test,你可以移除显式添加的testImplementation("org.springframework.security:spring-security-test:5.7.3"),避免手动版本管理带来的潜在冲突。

做完以上操作后,重新执行测试,问题应该就能解决。


内容的提问来源于stack exchange,提问作者Otourou Da Costa

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.21 06:48:19