【问题标题】:@EnableAutoConfiguration(exclude =...) on tests failed in Spring Boot 2.6.0@EnableAutoConfiguration(exclude =...) 在 Spring Boot 2.6.0 中的测试失败
【发布时间】:2021-12-30 23:14:06
【问题描述】:

我尝试将我的 data-mongo 示例项目升级到 Spring Boot 2.6.0。有一个针对 Testcontainers 运行的测试,我还包含了嵌入式 mongo dep 用于其他测试,因此我必须排除嵌入式 mongo 的 AutoConfiguration 以确保该测试在 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

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

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

【问题讨论】:

    标签: spring spring-boot spring-data spring-data-mongodb-reactive


    【解决方案1】:

    从 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

    【讨论】:

    • 我很了解 Spring Boot 2.6.0 的变化,知道如果在测试中使用嵌入式 mongo 必须设置 spring.mongodb.embedded.version,但这里的问题是 @EnableAutoConfiguration(exclude ="...") 不起作用@DataMongoTest.
    • 你弄明白了吗?我正在尝试使其适用于 Flapfoodle 的测试,但失败了。
    • @MattRaible 检查我的working example
    【解决方案2】:

    在升级到 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 {}
    

    【讨论】:

      【解决方案3】:

      只需添加:

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

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

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

      【讨论】:

      • 你也没有理解我原来的问题,我的目的是在我的测试中禁用嵌入式 mongo,它使用 testcontainers 在后台提供 Mongo 服务。
      • @Igor,请问您从哪里获得版本?我找不到可以看到不同版本的地方。
      • 这是个好问题,@RamPrakash。我花了一些时间寻找最新版本,但找不到太多。这是使用此版本的代码:github.com/spring-projects/spring-boot/blob/…
      猜你喜欢
      • 2018-09-21
      • 1970-01-01
      • 2018-01-04
      • 1970-01-01
      • 2019-12-26
      • 2023-03-23
      • 2017-07-02
      • 2021-10-02
      • 1970-01-01
      相关资源
      最近更新 更多