타 시스템 데이터베이스와 연동할 일이 있어 JUnit으로 테스트를 해보려는데 서버 구동이 안되고 오류가 났다.
org.springframework.beans.factory.UnsatisfiedDependencyException
...
;nested exception is java.lang.NullPointerException
대충 빈 생성/주입 과정에서 오류가 난 것 같은데 NullPointerException인걸 보면 뭔가를 못 가져오고 있는 것 같았다.
오류 발생 지점 소스 코드를 확인해보니 소스코드 중 서버 구동 시점에 활성화된 profile에 따라 분기 처리를 하는 부분에서 profile을 가져오지 못해 발생한 오류였다.
로컬 서버에서 테스트중이니 간단하게 어노테이션을 사용해 로컬 프로파일을 활성화해주면 되겠군!
방법1.
@ActivateProfiles(profiles="local")
@SpringBootTest
class ConnectionTest {
}
NullPointerException은 넘어갔지만 다른 곳에서 profile을 인식하지 못하는 오류가 또 발생했다.
Could not resolve placeholder 'spring.profiles.active' in value "${spring.profiles.active}"
오류 메시지만 보고도 @Value 어노테이션으로 active profile 설정을 가져오는 부분에서 오류가 발생했다는 걸 알 수 있었다.
@ActivateProfiles보다 @Value 어노테이션이 먼저 동작하는 듯 했다. 그래서 수정한 코드
방법2.
@SpringBootTest(properties={"spring.profiles.active:local"})
class ConnectionTest {
}
이렇게 프로퍼티를 설정해주니 서버가 정상 구동되었다.
'Develop > Spring' 카테고리의 다른 글
[React + Spring Boot] 블로그 구현 (0) | 2021.09.24 |
---|