Junit5 Platform properties

여러가지 있으나 나중에 필요할 때 찾아봅시다.

Junit5 확장 모델

확장 모델 클래스 만드는 방법

public class FindSlowTestExtension implements BeforeTestExecutionCallback, AfterTestExecutionCallback {

  private final long threshold;

  public FindSlowTestExtension(long threshold) {
    this.threshold = threshold;
  }

  @Override
  public void beforeTestExecution(ExtensionContext extensionContext) throws Exception {
    String testClassName = extensionContext.getRequiredTestClass().getName();
    String testMethodName = extensionContext.getRequiredTestMethod().getName();
    ExtensionContext.Store store = extensionContext.getStore(ExtensionContext.Namespace.create(testClassName, testMethodName));
    store.put("START_TIME", System.currentTimeMillis());
  }

  @Override
  public void afterTestExecution(ExtensionContext extensionContext) throws Exception {
    String testClassName = extensionContext.getRequiredTestClass().getName();
    String testMethodName = extensionContext.getRequiredTestMethod().getName();
    ExtensionContext.Store store = extensionContext.getStore(ExtensionContext.Namespace.create(testClassName, testMethodName));
    Long start_time = store.remove("START_TIME", long.class);
    long duration = System.currentTimeMillis() - start_time;
    if (duration > threshold) {
      System.out.printf("Please consider that %s#%s is too slow! (%dms)\n", testClassName, testMethodName, duration);
    }
  }
}

확장 모델 클래스 등록 방법

//방법 1 : @ExtendWith
@ExtendWith(FindSlowTestExtension.class)
public class StudyTest8 {

  //방법 2 : @RegisterExtension
  @RegisterExtension
  static FindSlowTestExtension findSlowTestExtension = new FindSlowTestExtension(1000L);

  @Test
  void create() throws InterruptedException {
    System.out.println("create ");
    Thread.sleep(2000);
  }

  @Test
  void create1() {
    System.out.println("create 1");
  }

  @Test
  void create2() {
    System.out.println("create 2 ");
  }
}

댓글남기기