Spring Boot 테스트 실행환경 분리하기

정지수 JisooJung·2021년 10월 15일
0

김영한님의 실전! 스프링 부트와 JPA 활용1 - 웹 애플리케이션 개발 강의를 듣다 테스트 환경이 제대로 분리되지 않는 문제가 있었다. 질문 목록에서 찾아보니 IDE 문제인 것 같았다.

인텔리제이에서는 test 하위에 application.yml을 만들어두면 자동으로 main 하위의 application.yml보다 우선적으로 적용된다. 그러나 vscode는 우선순위가 적용되지 않는다..ㅠㅠ 무조건 main 하위의 application.yml이 적용된다. (sts도 마찬가지인듯)

따라서 아래와 같이 환경을 분리해주었다.

1. application.yml에 테스트 환경 추가 (multi-document YAML)

  #application.yml
    spring:
        config.activate.on-profile: default

     ---
    spring:
        config.activate.on-profile: test

스프링 부트 2.4 이후 버전에서 spring.profiles는 deprecated 되었다. spring.config.activate.on-profile을 써주자!

2. 테스트 클래스에 @ActiveProfiles 추가

  //xxxTest.java
  @ExtendWith(SpringExtension.class) //Junit5
  @SpringBootTest
  @ActiveProfiles("test") // 괄호 안에 실행 환경을 명시해준다.
  public class xxxTest {
      ...
  }

위 두가지 코드를 적용하고 나면 지정한 테스트 환경으로 정상동작하는 것을 확인할 수 있다.


참고:

profile
Study&Work&Log

1개의 댓글

comment-user-thumbnail
2022년 3월 7일

좋은 글 감사합니다.

답글 달기