[SpringBoot] @TestPropertySource 애노테이션 사용시 yml 파일의 프로퍼티 불러오기

2022. 10. 31. 21:37문제해결

@TestPropertySource 애노테이션의 yml 파일에 저장된 프로퍼티 불러오기

@TestPropertySource 애노테이션의 locations 옵션을 사용하면 파일에 설정된 프로퍼티 속성을 불러올 수 있습니다. 그런데 locations 옵션 설명을 보면 지원하는 파일 형식은 *.properties 형식이나 *.xml 형식만을 지원합니다. 예를 들어 properties 형식의 파일을 불러온다고 가정하면 다음과 같이 정의할 수 있습니다.

@SpringBootTest
@TestPropertySource(locations = {"classpath:/application.properties"})
public class SpringBootApplicationTest {

하지만 yml 파일 같은 경우 다음과 같이 정의해야지 불러올 수 있습니다.

@SpringBootTest
@TestPropertySource(properties = {"spring.config.location = classpath:test.yml"})
public class SpringBootApplicationTest {

위와 같이 설정하면 spring.config.location 프로퍼티의 값으로 "classpath:test.yml" 값을 설정하기 때문에 yml 파일의 프로퍼티를 참조할 수 있습니다.