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

基于JUnit5+spring-test-mongo做集成测试时报invalid method reference错误

问题原因

你遇到的invalid method reference错误主要由以下两个配置问题导致:

  • Lombok版本冲突:build.gradle中主代码依赖声明了Lombok 1.18.20,测试依赖又重复声明了1.16.20的旧版本,版本不一致会导致getter、setter等方法无法正常生成,因此代码中引用的Bar::getId、Bar::getData不存在。
  • 缺少Lombok注解处理器配置:Gradle 5及以上版本需要显式声明Lombok的注解处理器依赖,否则Lombok注解不会生效,也会导致方法找不到的问题。

解决方案

第一步:修正build.gradle依赖配置

将dependencies部分替换为如下内容,统一Lombok版本并添加注解处理器声明:

dependencies {
    implementation 'org.projectlombok:lombok:1.18.20'
    annotationProcessor 'org.projectlombok:lombok:1.18.20'
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.0'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.0'
    testImplementation 'com.jupiter-tools:spring-test-mongo:0.15'
    testImplementation 'org.springframework.boot:spring-boot-starter-data-mongodb:2.5.4'
    testImplementation 'org.springframework.boot:spring-boot-starter-test:2.5.4'
    // 测试环境也需要配置Lombok注解处理器
    testAnnotationProcessor 'org.projectlombok:lombok:1.18.20'
}

第二步:开启IDE注解处理器(如果使用IntelliJ IDEA)

打开设置 → Build, Execution, Deployment → Compiler → Annotation Processors,勾选Enable annotation processing选项,保存后重新构建项目即可。

可选兼容写法

如果修改配置后仍有临时编译问题,可以把AssertJ的extracting写法替换为属性名字符串,避开方法引用的编译校验:

Assertions.assertThat(simpleDoc)
        .isNotNull()
        .extracting("id", "data");

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.03 17:57:03