티스토리 뷰

1. 테스트 클래스에 확장 모델을 적용하는 방법

  - 확장 모델을 만들어 테스트 클래스가 실행될 때 적용되도록 설정

  - 실행 시간이 일정 시간을 초과하는 경우 테스트의 경우 @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