5. Readable - Trouble Shooting
Problem
1. forEach, 변수사용 증가
2. 프로덕션 코드와 결합도 증가
Readable X
Automated X
Code Link
6. Readable - Trouble Shooting
Solution
1. 논리 조건 제거 (if, while, for, switch)
2. @ParameterizedTest 적용
3. 구현 세부 정보 보다는 최종 결과에 집중
Readable O
Automated O
Code Link
7. Readable - @ParameterizedTest
How to use
• Stream.of (
arguments(“테스트 데이터”),
arguments(“테스트 데이터”),
…
)
returnType : Stream<Arguments>
• Annotation 생성
• @ParameterizedTest
• @MethodSource(“provider 명”)
원하는 테스트 케이스 만큼 ROW 추가
0 0 0
1 2 3
2 4 6
• Provider Method 명 Mapping
9. Fast - StandAlone
StandAlone WebMVCTest
목적 단위 테스트 슬라이스 테스트
범위 Controller Spring MVC
컨텍스트 로딩 여부 N Y
어노테이션 Bean 생성 여
부
N N
• StandAlone : 하나 이상의 @Controller 인스턴스를 등록하고 최소한의 Spring MVC 인프라를 구성하여 사용할 수 있는 테스트
• WebMvcTest : Spring MVC 테스트에 초점을 맞춘 테스트
10. Fast - StandAlone
WebMvcTest vs StandAlone
Registered Bean Count = 218
Mocking Count = 19
Registered Bean Count = 0
Mocking Count = 4
Conclusion
MockBean의 개수가 줄어드는 효과
Spring Context Loading을 실행하지 않음으로써 성능 상의 이점
Controller에 대한 단위 테스트가 목적이라면 standAlone 권장
11. Fast - standAloneSetup
How to use
• 테스트 대상 Controller Mocking
• 각각의 테스트 실행 시 MockMvc에 standaloneSetup 설정
• standaloneSetup의 parameter에 테스트 대상 컨트롤러 지정
Simulation
standAlone
webMvcTest
15. Fast - Context Caching
Spring은 실행 시점에 테스트 구성에 따라 ApplicationContext 로딩
테스트 구성이 동일한 경우 Cache 에 담겨진 ApplicationContext 재 사용
테스트 구성이 같거나 비슷한 경우 같은 구성으로 묶어서 관리한다면 reload 을 피할 수 있다.
TestA
@Autowired
ServiceA
@Autowired
ServiceB
@Autowired
ServiceC
TestC
@Autowired
ServiceA
@Autowired
ServiceB
@Autowired
ServiceC
TestB
@Autowired
ServiceA
@Autowired
ServiceB
@Autowired
ServiceC
@Autowired
ServiceD
1 2 3
time
16. Fast - Context Caching
최초 로딩 후 reload X
1 2 3
AbstractTestConfig
@Autowired
ServiceA
@Autowired
ServiceB
@Autowired
ServiceC
@Autowired
ServiceD
TestA TestB TestC
time
time
18. Fast - Context Caching
Debug - DefaultCacheAwareContextLoaderDelegate
First Loading
ContextController02Test ContextController03Test
ContextController01Test ContextController03Test
19. Summary
• Readable, Automated
• 구현의 세부정보 보다는 최종 결과에 집중 (with @ParameterizedTest)
• Fast
• StandAlone - Unit Test
• Context Loading 제거
• Context Caching - Slice Test
• Context Loading 최소화