∞. 트러블 슈팅 + TIL

05. Spring Boot 2.4 Test 와 Profile

THE HEYDAZE 2021. 9. 8. 20:58
문제

application-app.yml

@SpringBootTest
class TokenProviderTest {

    @Autowired
    private MemberRepository memberRepository;

    @Autowired
    private TokenProvider tokenProvider;

    @Test
    void getToken() {

        Member member = Member.builder()
                .name("sout1217")
                .nickName("sout1217")
                .email("sout1217@naver.com")
                .providerId(UUID.randomUUID().toString())
                .provider(AuthProvider.naver)
                .profileImg("http://localhost:8080/images")
                .build();

        Member saveMember = memberRepository.save(member);

        UserPrincipal userPrincipal = UserPrincipal.create(saveMember);

        String token = tokenProvider.createToken(userPrincipal);

        System.out.println(token);
    }
}

 

테스트 실행화면

원인

 

tokenExpirationMsec 은 그나마 0으로 되어서 오류가 나지 않는다

 

test 패키지 resources 에 이미 application.yml 이 존재 하는 경우 해당 application.yml 을 사용해서 문제가 일어났다

해당 application.yml 에는 app property 설정이 되어있지 않음

 

해결

test/resources/application-local.yml 설정파일을 만들아 app 설정한 뒤 테스트 하고자 하는 클래스에 어떤 profile 로 실행할지 정하는 @AcitiveProfiles 를 넣어준다

 

 

@Profile 과 @ActiveProfiles 의 차이


@Profile 을 붙인 클래스는 해당 Profile 로 실행했을 때만 사용하겠다는 의미이고,

@ActiveProfiles 는 이 클래스에 해당 Profile 설정을 적용하겠다는 의미이다