Home > database >  Springboot 2.6.7 mongodb dependency not working on @SpringBootTest
Springboot 2.6.7 mongodb dependency not working on @SpringBootTest

Time:05-04

I've recently upgraded my springboot dependencies from version 2.5.4 to 2.6.7 here my gradle file

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
    }
}

The v2.6.7 gradle file its the same, just changing the sprinboot plugin version to 2.6.7 and springBootVersion variable to the same version, although I didn't had the variable before, just added when upgrading the version to avoid the dependency management issues but didn't work either.

I've tried also with v2.5.13 and it works, looks something coming from 2.6.7 version.

When doing a little research the cause might be related with mongodb drivers, still don't know why when starting the application works but when running the tests with @SpringBootTest it doesn't with the v2.6.7

I don't know if it helps here is the @SprinBootTest configuration I use, for both docker images I use the :latest version

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

Here the class that extends the previous one and uses the tests annotations

@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...
}

And here the exception thrown when executing the tests, (edited the stack trace to sum the issue, I can give the entire stack trace if required)

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.
    

CodePudding user response:

So finally caught the problem, it was failing due to the Intellij IDEA configuration.

In the Build,Execution,Deployment> Build Tools> Gradle in the option Run tests using: I had there selected Intellij IDEA, I changed it to Gradle(default) and now the tests works.

So nothing related to the version change.

Edited: I removed all the cached libraries and also the mongo docker image to avoid driver issues and got it working

  • Related