1. 상황
테스트를 만들며 스프링 부트를 띄워서 테스트를 진행해보기 위해 @SpringBootTest를 사용해 진행해보았다. 그러나 ApplicationContext를 로드하는 데에 실패했다는 에러 메세지가 나왔다.
하지만 프로젝트 진행 초기라 만들어둔 코드가 많지 않았고, 비슷한 다른 초기 프로젝트에서는 발생하지 않았던 에러라 원인이 더욱 궁금했다.
에러 메시지를 검색해보니 원인이 무척 다양해서 헤매던 중, 에러 로그를 찬찬히 읽어보다 데이터소스 부분까지 에러가 내려가는 것을 확인했다. 내가 건들인 밑단 설정을 곰곰이 생각해보던 중, 개발 환경을 application.yaml의 profile을 이용하여 다양하게 설정해둔 것이 기억났다!
그리고 찾아보니 profile을 테스트에서 따로 설정할 수 있다는 인터넷 게시글도 발견하여 적용하니 해결됐다.
2. 에러 메시지
java.lang.IllegalStateException: Failed to load ApplicationContext
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'someController' defined in file [C:\MyProject\~~~\someController.class]
~~~~
Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.zaxxer.hikari.HikariDataSource]:
3. 해결 방법
profile을 따로 적어주니 해결됐다. 나는 @SpringBootTest 어노테이션 위에 @ActiveProfiles("local") 을 붙여주었다..
@ActiveProfiles("local")
@SpringBootTest
class MyApplicationTests {
@Test
void contextLoads() {
}
}