목적
- 코딩을 하는 환경과 실제 프로그램이 동작하는 환경이 다른 경우가 대다수이다.
- 이 경우 통합 테스트 등을 진행하거나 모듈 테스트를 진행할 시에 로컬에서는 동작하나 리얼 혹은 알파에서는 동작하지 않도록 하고싶은 경우가 존재한다.
- 이러한 경우에 이용할 수 있는 방법을 공유한다.
- 필자는 spring boot 2.4.4에서 테스트를 진행하였으며 JUnit5에서 적용가능하다.
방법
해당 요건을 만족시키기 위해 필자는
@EnabledIfEnvironmentVariable
를 이용할 것이다. 해당 annotation 이외에도@DisabledIfEnvironmentVariable
,@Enabled
,@Disabled
등의 annotation을 이용할 수 있다. annotation 명에서 알 수 있다시피 필자가 사용한 annotation@EnabledIfEnvironmentVariable
는 어떤 환경변수가 설정되어 있을 때에만 테스트가 동작하도록 한다.@EnabledIfEnvironmentVariable
annotation을 다음과 같이 추가하였다. 이를 추가하여 intelliJ에서 테스트를 진행하면(intellJ 내부적으로 추가적인 환경변수 설정을 하지 않음) 테스트는 정상적으로 작동하는 것을 볼 수 있다.... import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable; ... ... @EnabledIfEnvironmentVariable( named="SPRING\_PROFILES\_ACTIVE", matches="default" ) public class MyTest { ...
profile을 다음과 같이 입력하여 리얼 배포용 build시 MyTest를 스킵할 수 있다.(필자는 gradle을 이용)
$ ./gradlew build -Dspring.profiles.active=deploy
참고
https://stackoverflow.com/questions/43851621/is-it-possible-to-mark-springboot-tests-so-they-run-only-when-certain-profile-is
https://docs.spring.io/spring-framework/docs/current/reference/html/testing.html#integration-testing-annotations-junit-jupiter
반응형
'기타' 카테고리의 다른 글
[AWS] Python으로 S3 Object(디렉터리 혹은 파일) 아래 파일 확인 (0) | 2021.08.03 |
---|---|
[AWS] Python으로 S3 Object(디렉터리 혹은 파일)의 용량 확인 (0) | 2021.07.08 |
[Ubuntu] git gui툴인 gitkraken설치 (0) | 2021.03.16 |
[Ubuntu] multiple workspaces and dual monitor (0) | 2020.12.22 |
[Ubuntu] pyqt5 및 qtdesigner 설치 (0) | 2020.12.21 |