@EnableAutoConfiguration(exclude =...) on tests failed in Spring Boot 2.6.0
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
.
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 {}
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.