Springboot 2.6.7 mongodb 依赖项在@SpringBootTest 上不起作用

Springboot 2.6.7 mongodb dependency not working on @SpringBootTest

我最近将我的 springboot 依赖项从版本 2.5.4 升级到 2.6.7 在这里我的 gradle 文件

v2.5.4

plugins {
id 'org.springframework.boot' version '2.5.4'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
id 'jacoco'
id 'com.netflix.dgs.codegen' version '5.0.6'
id("org.springdoc.openapi-gradle-plugin") version "1.3.4"
}

group 'my.package'
version '0.0.1'
sourceCompatibility = '17'

jacoco {
    toolVersion = '0.8.8'
}

configurations {
    developmentOnly
    runtimeClasspath {
        extendsFrom developmentOnly
    }
    compileOnly {
        extendsFrom annotationProcessor
    }
}
    
repositories {
    mavenCentral()
}
ext {
    mapstructVersion = "1.4.2.Final"
    lombokVersion = "1.18.20"
    queryDslVersion = "5.0.0"
    cucumberVersion ="7.3.2"
    springBootVersion ="2.5.4"
}
dependencies {
    compileOnly "org.mapstruct:mapstruct:${mapstructVersion}", "org.projectlombok:lombok:${lombokVersion}"
    annotationProcessor "org.mapstruct:mapstruct-processor:${mapstructVersion}", "org.projectlombok:lombok:${lombokVersion}"
    implementation 'org.springdoc:springdoc-openapi-ui:1.6.8'
    implementation 'org.springdoc:springdoc-openapi-security:1.6.8'
    implementation "com.querydsl:querydsl-core:${queryDslVersion}"
    implementation "com.querydsl:querydsl-mongodb:${queryDslVersion}"
    implementation "com.querydsl:querydsl-apt:${queryDslVersion}"
    implementation "com.querydsl:querydsl-sql:${queryDslVersion}"
    implementation 'org.imgscalr:imgscalr-lib:4.2'
    annotationProcessor("com.querydsl:querydsl-apt:${queryDslVersion}:general")
    implementation("org.springframework.boot:spring-boot-starter-actuator:${springBootVersion}",
            "org.springframework.boot:spring-boot-starter-security:${springBootVersion}",
            "org.springframework.boot:spring-boot-starter-data-mongodb:${springBootVersion}",
            "org.springframework.boot:spring-boot-starter-web:${springBootVersion}")
    implementation 'javax.validation:validation-api:2.0.1.Final'
    implementation 'com.sendgrid:sendgrid-java:4.0.1'
    implementation "org.springframework.boot:spring-boot-starter-data-redis:${springBootVersion}"
    implementation 'redis.clients:jedis'
    implementation 'io.jsonwebtoken:jjwt:0.9.1'
    implementation 'commons-io:commons-io:2.6'
    implementation 'commons-codec:commons-codec:1.9'
    //implementation 'javax.xml.bind:jaxb-api:2.3.1'
    implementation(platform("com.netflix.graphql.dgs:graphql-dgs-platform-dependencies:latest.release"))
    implementation "com.netflix.graphql.dgs:graphql-dgs-spring-boot-starter"
    implementation "com.netflix.graphql.dgs:graphql-dgs-pagination"
    implementation("com.netflix.graphql.dgs:graphql-dgs-extended-scalars")
    developmentOnly "org.springframework.boot:spring-boot-devtools:${springBootVersion}"

    testImplementation("org.springframework.boot:spring-boot-starter-test:${springBootVersion}")
    testImplementation "io.cucumber:cucumber-java8:${cucumberVersion}"
    testImplementation "io.cucumber:cucumber-junit:${cucumberVersion}"
    testImplementation "io.cucumber:cucumber-spring:${cucumberVersion}"
    testImplementation('org.testcontainers:testcontainers:1.17.1')
}

test {
    useJUnitPlatform()
}
configurations {
    developmentOnly
    runtimeClasspath {
        extendsFrom developmentOnly
    }
    compileOnly {
        extendsFrom annotationProcessor
    }
}

v2.6.7 gradle文件一样,只是把sprinboot插件版本改成2.6.7,springBootVersion变量也改成同一个版本,虽然我之前没有这个变量,只是添加的时候升级版本以避免依赖管理问题,但也没有用。

我也尝试过使用 v2.5.13 并且它有效,看起来来自 2.6.7 版本。

在做一些研究时,原因可能与 mongodb 驱动程序有关,仍然不知道为什么在启动应用程序时可以正常工作,但是当 运行 使用 @SpringBootTest 进行测试时不适用于 v2.6.7

我不知道它是否有帮助这里是我使用的@SprinBootTest配置,对于两个docker图像我使用:latest版本

@ContextConfiguration(initializers = {
    MongoDBTestContainer.MongoDBTestContainerInitializer.class,
    RedisTestContainer.RedisTestContainerInitializer.class})
public class ComponentBaseTest {
  basic variables and stuff, not relevant...
}

此处 class 扩展了前一个并使用了测试注释

@SpringBootTest(classes = {
        MyApp.class,
        ClassOnlyForTests.class,
        ClassOnlyForTests2.class,
        ClassOnlyForTests3.class,
        ClassOnlyForTests4.class,
        ClassOnlyForTests5.class,
        ClassOnlyForTests6.class}
        , webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ExtendWith(SpringExtension.class)
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@ActiveProfiles("component")
@DisplayName("BDD Test")
public class MyComponentTest extends ComponentBaseTest {
nothing relevant here...
}

这里是执行测试时抛出的异常,(编辑堆栈跟踪以总结问题,如果需要我可以提供整个堆栈跟踪)

Unsatisfied dependency expressed through constructor parameter 0;
 nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'defaultData' defined in file []: 
Unsatisfied dependency expressed through constructor parameter 1; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'myJpaRepository' defined in my.package.infrastructure.repository.jpa.MyJpaRepository defined in @EnableMongoRepositories declared on MyApp: Cannot resolve reference to bean 'mongoTemplate' while setting bean property 'mongoOperations';
 nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'mongoTemplate' defined in class path resource [org/springframework/boot/autoconfigure/data/mongo/MongoDatabaseFactoryDependentConfiguration.class]:
Unsatisfied dependency expressed through method 'mongoTemplate' parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'mongoDatabaseFactory' defined in class path resource [org/springframework/boot/autoconfigure/data/mongo/MongoDatabaseFactoryConfiguration.class]: 
Unsatisfied dependency expressed through method 'mongoDatabaseFactory' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mongo' defined in class path resource [org/springframework/boot/autoconfigure/mongo/MongoAutoConfiguration.class]: 
Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.mongodb.client.MongoClient]: Factory method 'mongo' threw exception;
 nested exception is java.lang.AbstractMethodError: Receiver class io.micrometer.core.instrument.binder.mongodb.MongoMetricsConnectionPoolListener does not define or inherit an implementation of the resolved method 'abstract void connectionPoolOpened(com.mongodb.event.ConnectionPoolOpenedEvent)' of interface com.mongodb.event.ConnectionPoolListener.
    

所以终于抓住了问题,由于Intellij IDEA配置失败。

在选项 Run tests using:Build,Execution,Deployment> Build Tools> Gradle 中,我选择了 Intellij IDEA,我将其更改为 Gradle(default),现在测试有效。

因此与版本更改无关。

已编辑: 我删除了所有缓存的库以及 mongo docker 图像以避免驱动程序问题并使其正常工作