Back-End/Spring

[Spring] mongoDB Test Error (Error creating bean with name 'embeddedMongoServer' defined in class path resource)

Henu 2022. 5. 10. 23:18
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2022-05-10 23:00:10.030 ERROR 7765 --- [    Test worker] o.s.boot.SpringApplication               : Application run failed

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'embeddedMongoServer' defined in class path resource [org/springframework/boot/autoconfigure/mongo/embedded/EmbeddedMongoAutoConfiguration.class]: Unsatisfied dependency expressed through method 'embeddedMongoServer' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'embeddedMongoConfiguration' defined in class path resource [org/springframework/boot/autoconfigure/mongo/embedded/EmbeddedMongoAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [de.flapdoodle.embed.mongo.config.MongodConfig]: Factory method 'embeddedMongoConfiguration' threw exception; nested exception is java.lang.IllegalStateException: Set the spring.mongodb.embedded.version property or define your own MongodConfig bean to use embedded MongoDB

위와 같은 에러가 뜨는 경우 해결 방법

 

밑줄 친 부분을 보니 내장 mongoDB를 쓰고 싶으면 spring.mongodb.embedded.version에 대한 property를 설정해주라는 이야기인 듯 하다.

 

찾아보니 spring boot 2.6 이후부터 spring.mongodb.embedded.version은 자동 구성된 임베디드 MongoDB를 사용하도록 설정해야하는것 같다.

 

test 폴더의 하위에 recources 폴더를 만들고

application.yml 파일을 아래와 같이 생성한다.

// test/resources/application.yml

spring:
  mongodb:
    embedded:
      version: 3.3.4

 

문서의 내용을 보면 production에서 사용할 mongodb의 버전과 embedded.mongo의 버전을 동일하게 해주면 된다고 한다.

 

그래서 현재(2022.05.10) 기준 spring data mongo는 3.3.4 버전을 지원하기 때문에 embedded 버전도 3.3.4로 맞춰주었다.

spring 공식문서

 

테스트 해보면 정상 동작할 것이다.

해결!

 

 

2022.05.10
springboot 2.6.7 기준
mongodb.embedded.version 으로 3.3.43.5.5, 3.6.5, latest 등이 사용 가능했다..