JUnit 4의 확장 모델은 @RunWith(Runner), TestRule, MethodRule.
JUnit 5의 확장 모델은 단 하나, Extension.
확장팩 등록 방법
확장팩 만드는 방법
참고
확장모델, 선언적인 등록 @ExtendWith
package com.example.testingweb.validatingforminput;
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 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);
String testMethodName = context.getRequiredTestMethod().getName();
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", testMethodName);
}
}
private ExtensionContext.Store getStore(ExtensionContext context) {
String testClassName = context.getRequiredTestClass().getName();
String testMethodName = context.getRequiredTestMethod().getName();
return context.getStore(ExtensionContext.Namespace.create(testClassName, testMethodName));
}
}
package com.example.testingweb.validatingforminput;
import org.junit.jupiter.api.*;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.extension.Extensions;
import org.junit.jupiter.api.extension.ParameterContext;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.aggregator.AggregateWith;
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
// @DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class)
// @TestInstance(TestInstance.Lifecycle.PER_CLASS) // Study Instance를 한 개만 만듬.
@ExtendWith(FindSlowTestExtension.class) // 선언적 등록
@TestMethodOrder(MethodOrderer.OrderAnnotation.class) // OrderAnnotation을 가지고 순서를 정해준다.
class StudyTest {
void create_new_Study() {
@SlowTest
@DisplayName("스터디 만들기 slow")
@Disabled
void create1_new_study_again() {
void create_new_study_again() throws InterruptedException {
Thread.sleep(1005L);
System.out.println(this);
System.out.println("create1 " + value++);
}
확장모델, @RegisterExtension
public class FindSlowTestExtension implements BeforeTestExecutionCallback, AfterTestExecutionCallback {
private static final long THRESHOLD = 1000L;
private long THRESHOLD;
public FindSlowTestExtension(long THRESHOLD) {
this.THRESHOLD = THRESHOLD;
}
@Override
public void beforeTestExecution(ExtensionContext context) throws Exception {
@ExtendWith(FindSlowTestExtension.class) // 선언적 등록
@TestMethodOrder(MethodOrderer.OrderAnnotation.class) // OrderAnnotation을 가지고 순서를 정해준다.
class StudyTest {
int value = 0;
@RegisterExtension
static FindSlowTestExtension findslowTestExtension = new FindSlowTestExtension(1000L);
@Order(1)
@SlowTest
@Test
@DisplayName("스터디 만들기 slow")
@Disabled
void create_new_study_again() throws InterruptedException {