@EnableAutoConfiguration(exclude =...) on tests failed in Spring Boot 2.6.0
Asked Answered
D

4

15

I tried to upgrade my data-mongo example project to Spring Boot 2.6.0. There is a test designed to run against Testcontainers, I also included the embedded mongo dep for other tests, so I have to exclude the AutoConfiguration for embedded mongo to make sure this test working on Docker/testcontainers.

The following configuration worked well with Spring Boot 2.5.6.


@DataMongoTest
@ContextConfiguration(initializers = {MongodbContainerInitializer.class})
@EnableAutoConfiguration(exclude = EmbeddedMongoAutoConfiguration.class)
@Slf4j
@ActiveProfiles("test")
public class PostRepositoryTest {}

But after upgrading to Spring Boot 2.6.0 and running the application, I got the exception like this.

[           main] s.c.a.AnnotationConfigApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: o
rg.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'embeddedMongoServer' defined in class path resource [org/springframework/boot/autoconfig
ure/mongo/embedded/EmbeddedMongoAutoConfiguration.class]: Unsatisfied dependency expressed through method 'embeddedMongoServer' parameter 0; nested exception is org.springframework.bea
ns.factory.BeanCreationException: Error creating bean with name 'embeddedMongoConfiguration' defined in class path resource [org/springframework/boot/autoconfigure/mongo/embedded/Embed
dedMongoAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [de.flap
doodle.embed.mongo.config.MongodConfig]: Factory method 'embeddedMongoConfiguration' threw exception; nested exception is java.lang.IllegalStateException: Set the spring.mongodb.embedd
ed.version property or define your own MongodConfig bean to use embedded MongoDB

Obviously, @EnableAutoConfiguration(exclude =...) did not affect the context in tests when upgrading to Spring Boot 2.6.0.

Update: Temporarily resolved it, see my answer below.

Damsel answered 20/11, 2021 at 15:35 Comment(0)
D
7

Use @ImportAutoConfiguration(exclude = ...) or @DataMongoTest(excludeAutoConfiguration = ...) on test classes to overcome this barrier when upgrading to Spring Boot 2.6.0.

@DataMongoTest
@ImportAutoConfiguration(exclude = EmbeddedMongoAutoConfiguration.class)
//other config are ommitted
public class PostRepositoryTest {}

//or 
@DataMongoTest(excludeAutoConfiguration = EmbeddedMongoAutoConfiguration.class)
public class PostRepositoryTest {}
Damsel answered 21/12, 2021 at 12:47 Comment(0)
V
20

Just add:

@TestPropertySource(properties = "spring.mongodb.embedded.version=3.5.5")

annotation before your Unit Test and it will start working.

@Henning's answer has a good explanation of why you need this.

Vacancy answered 22/12, 2021 at 2:22 Comment(3)
You did not understand my original question as well, my purpose is disabling embedded mongo in my test which used testcontainers to serve a Mongo service in the background.Damsel
@Igor, can I ask you from where get the version? I could not find the place where I could see different versions.Flirtation
It is a good question, @RamPrakash. I have spent some time looking for the latest version but was not able to find much. Here is the code where this version is used: github.com/spring-projects/spring-boot/blob/…Vacancy
C
11

As of Spring Boot 2.6, the property spring.mongodb.embedded.version must be set to use the auto-configured embedded MongoDB. It's mentioned in the release notes: https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.6-Release-Notes#embedded-mongo

This is also what the error message you posted, advises to do: Set the spring.mongodb.embedd ed.version property or define your own MongodConfig bean to use embedded MongoDB

The annotation @DataMongoTest is meta-annotated with @ImportAutoConfiguration and @AutoConfigureDataMongo, and is designed to trigger auto-configuration of MongoDB unless explicitly disabled as you do in the working configuration examples.

In your first configuration example, the annotation @EnableAutoConfiguration(exclude = EmbeddedMongoAutoConfiguration.class) does not override this effect of @DataMongoTest.

With Spring Boot 2.5.6, the auto-configured MongodConfig bean is most likely also part of the application context but not effectively used. But this depends on the rest of the code and in particular on the MongodbContainerInitializer.

Conic answered 20/11, 2021 at 16:43 Comment(3)
I know well about the changes of Spring Boot 2.6.0 and know it is a must to set spring.mongodb.embedded.version if using embedded mongo in tests, but here the problem concern is @EnableAutoConfiguration(exclude ="...") does not work with @DataMongoTest.Damsel
Did you get this figured out? I'm trying to make it work for testing with flapfoodle, but failing.Pyroelectricity
@MattRaible check my working example.Damsel
D
7

Use @ImportAutoConfiguration(exclude = ...) or @DataMongoTest(excludeAutoConfiguration = ...) on test classes to overcome this barrier when upgrading to Spring Boot 2.6.0.

@DataMongoTest
@ImportAutoConfiguration(exclude = EmbeddedMongoAutoConfiguration.class)
//other config are ommitted
public class PostRepositoryTest {}

//or 
@DataMongoTest(excludeAutoConfiguration = EmbeddedMongoAutoConfiguration.class)
public class PostRepositoryTest {}
Damsel answered 21/12, 2021 at 12:47 Comment(0)
L
0

Thank you my issue got Resolved

org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'syncClientServerWrapper' defined in class path resource [de/flapdoodle/embed/mongo/spring/autoconfigure/EmbeddedMongoAutoConfiguration$SyncClientServerWrapperConfig.class]:
Unsatisfied dependency expressed through method 'syncClientServerWrapper' parameter 0: Error creating bean with name 'version' defined in class path resource [de/flapdoodle/embed/mongo/spring/autoconfigure/EmbeddedMongoAutoConfiguration.class]: Failed to instantiate [de.flapdoodle.embed.mongo.distribution.IFeatureAwareVersion]:
Factory method 'version' threw exception with message: Set the de.flapdoodle.mongodb.embedded.version property or define your own IFeatureAwareVersion bean to use embedded MongoDB

I have used this syntax

@SpringBootTest
@WireMockTest(httpPort = 9878)
@EnableAutoConfiguration(exclude = EmbeddedMongoAutoConfiguration.class)
Lymphosarcoma answered 4/5 at 15:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.