@EnableAutoConfiguration(exclude =...) 在 Spring Boot 2.6.0 中测试失败

Han*_*tsy 15 spring spring-data spring-boot spring-data-mongodb-reactive

我尝试将我的 data-mongo 示例项目升级到 Spring Boot 2.6.0。有一个设计用于针对 Testcontainers 运行的测试,我还包含了用于其他测试的嵌入式 mongo dep,因此我必须排除嵌入式 mongo 的自动配置,以确保此测试在 Docker/testcontainers 上运行。

以下配置适用于 Spring Boot 2.5.6


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

但是升级到 Spring Boot 2.6.0 并运行应用程序后,我得到了这样的异常。

[           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
Run Code Online (Sandbox Code Playgroud)

显然,@EnableAutoConfiguration(exclude =...)升级到 Spring Boot 2.6.0 时并没有影响测试中的上下文。

更新:暂时解决了,请参阅下面我的回答。

Igo*_*hyn 20

只需添加:

@TestPropertySource(properties = "spring.mongodb.embedded.version=3.5.5")
Run Code Online (Sandbox Code Playgroud)

在单元测试之前进行注释,它将开始工作。

@Henning 的答案很好地解释了为什么你需要这个。


Hen*_*ing 11

从 Spring Boot 2.6 开始,spring.mongodb.embedded.version必须将该属性设置为使用自动配置的嵌入式 MongoDB。发行说明中提到:https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.6-Release-Notes#embedded-mongo

这也是您发布的错误消息建议执行的操作:Set the spring.mongodb.embedd ed.version property or define your own MongodConfig bean to use embedded MongoDB

该注释@DataMongoTest使用@ImportAutoConfiguration和进行元注释@AutoConfigureDataMongo,旨在触发 MongoDB 的自动配置,除非像在工作配置示例中那样明确禁用。

在您的第一个配置示例中,注释@EnableAutoConfiguration(exclude = EmbeddedMongoAutoConfiguration.class)不会覆盖@DataMongoTest.

在 Spring Boot 2.5.6 中,自动配置的MongodConfigbean 很可能也是应用程序上下文的一部分,但未得到有效使用。但这取决于代码的其余部分,特别是MongodbContainerInitializer.


Han*_*tsy 7

升级到 Spring Boot 2.6.0 时,在测试类上使用@ImportAutoConfiguration(exclude = ...)或可以克服此障碍。@DataMongoTest(excludeAutoConfiguration = ...)

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

//or 
@DataMongoTest(excludeAutoConfiguration = EmbeddedMongoAutoConfiguration.class)
public class PostRepositoryTest {}
Run Code Online (Sandbox Code Playgroud)