• 카테고리

    질문 & 답변
  • 세부 분야

    백엔드

  • 해결 여부

    미해결

@TestPropertySource, @SpringBootTest 우선순위 관련 질문드립니다.

18.10.09 03:20 작성 조회수 3.51k

0

안녕하세요.

@TestPropertySource@SpringBootTest 우선순위 테스트 하다가

궁금한 점이 있어 질문을 남기게 되었습니다.

 

@TestPropertySource(properties = "")
@SpringBootTest(properties = "")

에서는 @TestPropertySource 의 우선순위가 더 높지만,

 

@TestPropertySource(locations = "")
@SpringBootTest(properties = "")

에서는 @SpringBootTest의 우선순위가 더 높은 것이 맞는지요?

 

문서를 확인해보았으나 부족한 영어 능력 탓인지 해당 내용을 찾지 못하였습니다. ㅠㅠ

아래는 제가 했던 Test 코드 입니다.

@TestPropertySource(properties = "from.route=@TestPropertySource")
@SpringBootTest(properties = "from.route=@SpringBootTest")
public class ApplicationTest {
    @Autowired
    private Environment environment;

    @Test
    public void testProperties() {
        assertThat(environment.getProperty("from.route"))
                .isEqualTo("@TestPropertySource");
   }
}

Test 통과합니다.

 

@TestPropertySource(locations = "classpath:/ApplicationTest.properties")
@SpringBootTest
public class ApplicationTest {
    @Autowired
    private Environment environment;

    @Test
    public void testProperties() {
        assertThat(environment.getProperty("from.route"))
                .isEqualTo("@TestPropertySource");
    }
}

Test 통과합니다.

 

@TestPropertySource(locations = "classpath:/ApplicationTest.properties")
@SpringBootTest(properties = "from.route=@SpringBootTest")
public class ApplicationTest {
    @Autowired
    private Environment environment;

    @Test
    public void testProperties() {
        assertThat(environment.getProperty("from.route"))
                .isEqualTo("@TestPropertySource");
    }
}

Test 실패합니다.
 

org.junit.ComparisonFailure:

Expected :"@TestPropertySource"

Actual :"@SpringBootTest"

답변 2

·

답변을 작성해보세요.

0

황준오님의 프로필

황준오

질문자

2018.10.09

답변 감사합니다.
제가 질문을 제대로 전달하지 못한것 같네요.
제가 테스트한 대로는 우선순위는 다음과 같습니다.
 
@TestPropertySource 의 우선순위가 @SpringBootTest 보다 높다.
하지만, @TestPropertySource(locations="") 의 경우에는 @SpringBootTest 보다 낮다. 입니다.  
 
제가 계속 헛소리하는 건 아닐까 걱정되지만..
@SpringBootTest#properties설정@TestPropertyTest#locations설정 을 계속 덮어버립니다.
 
따라서

우선순위

1. @TestPropertySource#properties 설정

2. @SpringBootTest#properties설정

3. @TestPropertyTest#locations설정

라고 생각되어서 질문을 남기게 되었습니다.

0

문서에 따르면 우선 순위는 다음과 같습니다.

1. devtools 글로벌 설정

2 @TestPropertySource 애노테이션 설정

3. @SpringBootTest#properties 설정

문서는 여기를 참고하세요.

https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-external-config

좋은 질문 주셔서 감사합니다.