JunsuLime / spring-cloud-native-explorer

Spring cloud explorer
1 stars 0 forks source link

스프링 부트의 단위테스트 / 모의기법 #20

Closed JunsuLime closed 3 years ago

JunsuLime commented 3 years ago

스프링부트 어플리케이션 통합테스트는 "여러 컴포넌트의 연동을 통합하기 위해 스프링 컨텍스트를 필요로 한다."

https://12factor.net/dev-prod-parity "개발/운영 짝맞춤": 개발 환경과 운영 환경의 차이를 최소화해야한다. (테스트 환경또한 동일하게 적용) 통합테스트 구성 시, 외부 의존성을 사용할 수 없을 경우 이를 mock 으로 대체할 수 있다.

-> 환경의 차이를 최소화 한 상태에서 테스트 할 수 있다.

Testing in Spring Framework: https://docs.spring.io/spring-framework/docs/current/reference/html/testing.html#testing Testing in Spring Boot: https://docs.spring.io/spring-boot/docs/current/reference/html/features.html#features.testing

Focus on Testing Spring Boot Applications

A Spring Boot application is a Spring ApplicationContext, so nothing very special has to be done to test it beyond what you would normally do with a vanilla Spring context.

앗 이건 저번에 테스트 속도 줄이기 보면서 유의할 점

Spring’s test framework caches application contexts between tests. Therefore, as long as your tests share the same configuration (no matter how it is discovered), the potentially time-consuming process of loading the context happens only once.

Spring Test 에서는 테스트 슬라이스라는 개념을 제공하여, 원하는 요소만 테스트 가능하게 하였다. 예시) DataJpaTest, DataRedisTest, JsonTest 등등..

JunsuLime commented 3 years ago

"테스트 결과에 대한 확신과 반복의 속도는 트레이드오프 관계이다."

JunsuLime commented 3 years ago

근본적으로 접근해보자. "테스트"는 왜 작성하는가? -> 테스트는 빠른 피드백 순환을 가능하게 하는 가장 중요한 수단이다.

12factor 의 개발/운영 짝맞춤에서는 개발환경과 운영환경의 차이를 최소화해야한다고 말한다. https://12factor.net/dev-prod-parity

테스트 환경또한 개발환경 중 하나이며 이 또한 최대한 비슷하게 구성하여, 이슈사항을 줄여나가야한다.

이를 위해 스프링 부트는 테스트 starter 에 모의 라이브러리를 제공하며, 외부 의존성을 모의 기능으로 대체할 수 있다.