application test : junit5 - Extension Model

This commit is contained in:
haerong22
2021-02-25 23:05:20 +09:00
parent f8180e4bf5
commit 68f74b0025
3 changed files with 163 additions and 0 deletions

View File

@@ -0,0 +1,57 @@
package com.example.apptest;
import org.junit.jupiter.api.extension.AfterTestExecutionCallback;
import org.junit.jupiter.api.extension.BeforeTestExecutionCallback;
import org.junit.jupiter.api.extension.ExtensionContext;
import java.lang.reflect.Method;
public class FindSlowTestExtension implements BeforeTestExecutionCallback, AfterTestExecutionCallback {
private final long THRESHOLD;
public FindSlowTestExtension(long THRESHOLD) {
this.THRESHOLD = THRESHOLD;
}
// 테스트 실행 전에 실행하는 메소드
@Override
public void beforeTestExecution(ExtensionContext context) throws Exception {
// 스토어 생성
ExtensionContext.Store store = getStore(context);
// store 에 값 저장하기
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);
// 스토어에서 값을 꺼내 실행 시간 구하기
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", requiredTestMethod.getName());
}
}
// 스토어 생성 메소드
private ExtensionContext.Store getStore(ExtensionContext context) {
// 테스트 클래스 이름 가져오기
String testClassName = context.getRequiredTestClass().getName();
// 테스트 메소드 이름 가져오기
String testMethodName = context.getRequiredTestMethod().getName();
// 스토어 생성
return context.getStore(ExtensionContext.Namespace.create(testClassName, testMethodName));
}
}

View File

@@ -0,0 +1,53 @@
package com.example.apptest;
import org.junit.jupiter.api.extension.AfterTestExecutionCallback;
import org.junit.jupiter.api.extension.BeforeTestExecutionCallback;
import org.junit.jupiter.api.extension.ExtensionContext;
import java.lang.reflect.Method;
public class FindSlowTestExtension_2 implements BeforeTestExecutionCallback, AfterTestExecutionCallback {
private static final long THRESHOLD = 1000L;
// 테스트 실행 전에 실행하는 메소드
@Override
public void beforeTestExecution(ExtensionContext context) throws Exception {
// 스토어 생성
ExtensionContext.Store store = getStore(context);
// store 에 값 저장하기
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);
// 스토어에서 값을 꺼내 실행 시간 구하기
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", requiredTestMethod.getName());
}
}
// 스토어 생성 메소드
private ExtensionContext.Store getStore(ExtensionContext context) {
// 테스트 클래스 이름 가져오기
String testClassName = context.getRequiredTestClass().getName();
// 테스트 메소드 이름 가져오기
String testMethodName = context.getRequiredTestMethod().getName();
// 스토어 생성
return context.getStore(ExtensionContext.Namespace.create(testClassName, testMethodName));
}
}

View File

@@ -0,0 +1,53 @@
package com.example.apptest;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.extension.RegisterExtension;
// 확장 모델을 필드에서 등록
class StudyExtensionTest_2 {
@RegisterExtension
static FindSlowTestExtension findSlowTestExtension
= new FindSlowTestExtension(1000L);
@SlowTest
void test_38() throws InterruptedException {
Thread.sleep(1500L);
System.out.println("extension test: @SlowTest");
}
@Test
void test_37() {
System.out.println("extension test: fast test");
}
@Test
void test_36() throws InterruptedException {
Thread.sleep(1500L);
System.out.println("extension test: slow test");
}
}
// 확장 모델을 어노테이션으로 사용
@ExtendWith(FindSlowTestExtension_2.class)
public class StudyExtensionTest {
@SlowTest
void test_35() throws InterruptedException {
Thread.sleep(1500L);
System.out.println("extension test: @SlowTest");
}
@Test
void test_34() {
System.out.println("extension test: fast test");
}
@Test
void test_33() throws InterruptedException {
Thread.sleep(1500L);
System.out.println("extension test: slow test");
}
}