Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Excluding EmbededMongoAutoConfiguration failed in Spring Boot 2.6.0

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.

like image 711
Hantsy Avatar asked Nov 20 '21 15:11

Hantsy


People also ask

Which version of spring does spring boot 2.6 use?

Spring Boot 2.6 moves to new versions of several Spring projects: Spring Data 2021.1. Spring HATEOAS 1.4. Spring AMQP 2.4.

What's new in Spring Boot 2. 6 6?

This release includes 6 bug fixes, documentation improvements, and dependency upgrades. Thanks to all those who have contributed with issue reports and pull requests.

What version of Spring framework does spring boot use?

Spring Boot 2.0 builds on and requires Spring Framework 5.


Video Answer


3 Answers

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.

like image 157
Igor Kanshyn Avatar answered Oct 19 '22 22:10

Igor Kanshyn


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.

like image 21
Henning Avatar answered Oct 19 '22 23:10

Henning


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 {}
like image 45
Hantsy Avatar answered Oct 19 '22 22:10

Hantsy