Intellij 2022.01.03, java 8, springboot 2.7.4, gradle 환경에서 작업하였습니다.
개요 |
spring mvc 환경에서 개발을 하다보면 실제 기능을 위한 작업과 기능을 테스트하기 위한 작업을 구분하여야 할 때가 있습니다. 저의 경우 실제 운영하는데에선 MariaDB가 사용되었고 테스트 환경에서는 In memory DB인 H2 데이터베이스를 사용하였습니다.
저는 두 개발환경을 두 개의 application.yml 파일을 둬서 관리하였습니다.
https://unagi-zoso.tistory.com/219
[Spring] Profile을 통해 다양한 application.yml 설정 운용하기
개요 예시 테스트 개요 서비스를 만들다보면 local, develop, product 등 서비스가 실행되는 환경이 다양할 수 있습니다. 저같은 경우에는 서비스를 구축하며 develop 환경에서 사용될 MariaDB와 test 환경
unagi-zoso.tistory.com
과정 |
각 application-dev.yml, application-local.yml 파일 마다 설정을 하고
저는 기본 application 파일을 개발환경인 dev로 하였습니다.
# application.yml
spring:
profiles:
active: dev # default application 파일의 이름
# ex application-dev.yml -> dev
이제 테스트 환경에선 application-local.yml 파일이 적용되어야하니 이 때는 다음과 같은 어노테이션이 사용됩니다.
@Profile, @ActiveProfiles
@Profile은 테스트 함수 마다 사용할 수 있으며 각 테스트 함수마다 원하는 파일을 적용시킬 수 있습니다.
@ActiveProfiles는 테스트 클래스 전체에 원하는 파일을 적용시킬 수 있습니다.
두 어노테이션 관계상 @Profile의 우선순위가 더 높습니다.
@ActiveProfiles("local")
@DataJpaTest
class PostRepositoryTest {
@Autowired
PostRepository postRep;
@Autowired
EntityManager em;
... 생략
@DisplayName("1. post 생성")
@Profile("dev")
@Test
void test_1(){
... 생략
읽어주셔서 감사합니다.
부족한 점이 있다면 부디 알려주시면 감사하겠습니다.
'언어와 프레임워크 > Spring' 카테고리의 다른 글
[Spring] JUnit5와 생성자 DI (0) | 2022.09.30 |
---|---|
[Spring] Spring Security와 h2-console 403 에러 (0) | 2022.09.30 |
[Spring] Profile을 통해 다양한 application.yml 설정 운용하기 (1) | 2022.09.30 |
[Spring] Security WebSecurityConfigurerAdapter Deprecated 대체하기 (0) | 2022.09.09 |
댓글