티스토리 뷰
Test/JUnit 5
[JUnit 5] 10. JUnit 5 확장 모델(@ExtendWith, @RegisterExtension)
mandykr 2021. 12. 9. 14:211. 테스트 클래스에 확장 모델을 적용하는 방법
- 확장 모델을 만들어 테스트 클래스가 실행될 때 적용되도록 설정
- 실행 시간이 일정 시간을 초과하는 경우 테스트의 경우 @SlowTest 어노테이션을 붙이라는 메세지 출력
- BeforeTestExecutionCallback, AfterTestExecutionCallback 을 구현하는 클래스를 정의
테스트 실행 전(beforeTestExecution)과
테스트 실행 후(afterTestExecution)의 시간을 비교해 THRESHOLD를 초과하는 경우 메세지가 출력되도록
1) @ExtendWith
- 선언적으로 등록
- 커스터마이징이 불가능
@ExtendWith(FindSlowTestExtension.class)
class StudyTest {
@Test
@DisplayName("스터디 만들기 1")
void create_study_1() throws InterruptedException {
System.out.println("스터디 만들기 1");
Thread.sleep(1005L);
}
@SlowTest
@DisplayName("스터디 만들기 2")
void create_study_2() throws InterruptedException {
System.out.println("스터디 만들기 2");
Thread.sleep(1005L);
}
}
public class FindSlowTestExtension implements BeforeTestExecutionCallback, AfterTestExecutionCallback {
private static final long THRESHOLD = 1000L;
@Override
public void beforeTestExecution(ExtensionContext context) throws Exception {
ExtensionContext.Store store = getStore(context);
store.put("START_TIME", System.currentTimeMillis());
}
@Override
public void afterTestExecution(ExtensionContext context) throws Exception {
Method requiredTestMethod = context.getRequiredTestMethod();
SlowTest annotation = requiredTestMethod.getAnnotation(SlowTest.class);
ExtensionContext.Store store = getStore(context);
String testMethodName = requiredTestMethod.getName();
Long start_time = store.remove("START_TIME", long.class);
long duration = System.currentTimeMillis() - start_time;
if (duration > THRESHOLD && annotation == null) {
System.out.printf("Please consider mark method [%s] with @SlowTest.\n", testMethodName);
}
}
private ExtensionContext.Store getStore(ExtensionContext context) {
String testClassName = context.getRequiredTestClass().getName();
String testMethodName = context.getRequiredTestMethod().getName();
ExtensionContext.Store store = context.getStore(ExtensionContext.Namespace.create(testClassName, testMethodName));
return store;
}
}
>> 실행 결과
2) @RegisterExtension
- 프로그래밍 등록
- 커스터마이징 가능
- THRESHOLD를 생성자를 통해 설정할 수 있다.
class StudyTest {
@RegisterExtension
static FindSlowTestExtension findSlowTestExtension = new FindSlowTestExtension(1000L);
@Test
@DisplayName("스터디 만들기 1")
void create_study_1() throws InterruptedException {
System.out.println("스터디 만들기 1");
Thread.sleep(1005L);
}
@SlowTest
@DisplayName("스터디 만들기 2")
void create_study_2() throws InterruptedException {
System.out.println("스터디 만들기 2");
Thread.sleep(1005L);
}
}
public class FindSlowTestExtension implements BeforeTestExecutionCallback, AfterTestExecutionCallback {
private long THRESHOLD;
public FindSlowTestExtension(long THRESHOLD) {
this.THRESHOLD = THRESHOLD;
}
@Override
public void beforeTestExecution(ExtensionContext context) throws Exception {
ExtensionContext.Store store = getStore(context);
store.put("START_TIME", System.currentTimeMillis());
}
@Override
public void afterTestExecution(ExtensionContext context) throws Exception {
Method requiredTestMethod = context.getRequiredTestMethod();
SlowTest annotation = requiredTestMethod.getAnnotation(SlowTest.class);
ExtensionContext.Store store = getStore(context);
String testMethodName = requiredTestMethod.getName();
Long start_time = store.remove("START_TIME", long.class);
long duration = System.currentTimeMillis() - start_time;
if (duration > THRESHOLD && annotation == null) {
System.out.printf("Please consider mark method [%s] with @SlowTest.\n", testMethodName);
}
}
private ExtensionContext.Store getStore(ExtensionContext context) {
String testClassName = context.getRequiredTestClass().getName();
String testMethodName = context.getRequiredTestMethod().getName();
ExtensionContext.Store store = context.getStore(ExtensionContext.Namespace.create(testClassName, testMethodName));
return store;
}
}
3) 자동 등록
- 자바 ServiceLoader 이용
- 명시적이지 않아 문제가 생길 수 있음
- 참고
출처
https://www.inflearn.com/course/the-java-application-test 더 자바, 애플리케이션을 테스트하는 다양한 방법(백기선)
728x90
'Test > JUnit 5' 카테고리의 다른 글
[JUnit 5] 9. junit-platform.properties (0) | 2021.12.09 |
---|---|
[JUnit 5] 8. 테스트 인스턴스(@TestInstance)와 테스트 순서(@TestMethodOrder) (0) | 2021.12.09 |
[JUnit 5] 7. 테스트 반복하기(@RepeatedTest, @ParameterizedTest) (0) | 2021.12.08 |
[JUnit 5] 5. 태깅과 필터링 (0) | 2021.12.08 |
[JUnit 5] 4. 조건에 따라 테스트 실행하기(assumeTrue, assumingThat, @Enabled___ 와 @Disabled___) (0) | 2021.12.08 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- java8
- Spring
- kafka
- JPA
- Git
- MySQL
- named query
- 트랜잭셔널 아웃박스 패턴
- Spring Data JPA
- 학습 테스트
- H2
- 클린코드
- 마이크로서비스 패턴
- ATDD
- HTTP 헤더
- Spring Boot
- spring rest docs
- 육각형 아키텍처
- Ubiquitous Language
- 폴링 발행기 패턴
- 이벤트 스토밍
- mockito
- http
- clean code
- 계층형 아키텍처
- 스프링 예외 추상화
- 도메인 모델링
- Stream
- TDD
- 스프링 카프카 컨슈머
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 | 31 |
글 보관함