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

问题描述 投票:0回答:4

我尝试将我的 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 {}

但是升级到 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

显然,升级到 Spring Boot 2.6.0 时,

@EnableAutoConfiguration(exclude =...)
并没有影响测试中的上下文。

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

spring spring-boot spring-data spring-data-mongodb-reactive
4个回答
20
投票

只需添加:

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

注释之前您的单元测试,它将开始工作。

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


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 中,自动配置的

MongodConfig
bean 很可能也是应用程序上下文的一部分,但未得到有效使用。但这取决于代码的其余部分,特别是
MongodbContainerInitializer


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

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)
© www.soinside.com 2019 - 2024. All rights reserved.