programing

@Spring Boot 2.6.0에서 실패한 테스트에서 자동 구성 사용(=... 제외)

starjava 2023. 7. 21. 20:26
반응형

@Spring Boot 2.6.0에서 실패한 테스트에서 자동 구성 사용(=... 제외)

저는 제 데이터-몽고 예제 프로젝트를 Spring Boot 2.6.0으로 업그레이드하려고 했습니다.테스트 컨테이너에 대해 실행되도록 설계된 테스트가 있으며, 다른 테스트를 위해 내장된 mongo dep도 포함했기 때문에 이 테스트가 도커/테스트 컨테이너에서 작동하는지 확인하기 위해 내장된 mongo에 대한 자동 구성을 제외해야 합니다.

다음 구성은 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으로 업그레이드할 때 테스트의 컨텍스트에 영향을 주지 않았습니다.

업데이트: 일시적으로 해결되었습니다. 아래 답변을 참조하십시오.

추가만 하면 됩니다.

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

단위 테스트 전에 주석을 추가하면 작동이 시작됩니다.

@헤닝의 대답은 당신이 왜 이것이 필요한지에 대한 좋은 설명을 가지고 있습니다.

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그리고.@AutoConfigureDataMongoMongoDB의 자동 구성을 트리거하도록 설계되었으며, 작업 구성 예제에서와 같이 명시적으로 비활성화되지 않은 경우.

첫 번째 구성 예에서 주석은@EnableAutoConfiguration(exclude = EmbeddedMongoAutoConfiguration.class)의 효과를 무시하지 않습니다.@DataMongoTest.

Spring Boot 2.5.6의 경우 자동 구성됨MongodConfig빈은 응용프로그램 컨텍스트의 일부이지만 효과적으로 사용되지 않을 가능성이 높습니다.하지만 이것은 코드의 나머지 부분에 따라, 특히MongodbContainerInitializer.

사용하다@ImportAutoConfiguration(exclude = ...)또는@DataMongoTest(excludeAutoConfiguration = ...)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 {}

언급URL : https://stackoverflow.com/questions/70047380/enableautoconfigurationexclude-on-tests-failed-in-spring-boot-2-6-0

반응형