Junit5의 Tag

기본 태그

@Test
@DisplayName("Tagging1")
@Tag("fast")
void tagging1() {
    System.out.println("fast");
}

@Test
@DisplayName("Tagging2")
@Tag("slow")
void tagging2() {
    System.out.println("slow");
}

커스텀 태그

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Test
@Tag("fast")
public @interface FastTest {
}

@FastTest
@DisplayName("FastTest")
void custom_tagging() {
    System.out.println("fast custom");
}

@SlowTest
@DisplayName("SlowTest")
void custom_tagging2() {
    System.out.println("slow custom");
}

intellij에서 tag 설정하는 법

intellij-tags.png

Junit5의 반복하기

기본 반복

class StudyTest {

  private final Study study = new Study(10);

  @RepeatedTest(10)
  void repeatTest1() {
    System.out.println("repeatTest");
  }

  @RepeatedTest(10)
  void repeatTest2(RepetitionInfo info) {
    System.out.println("repeatTest " + info.getCurrentRepetition() + "/" + info.getTotalRepetitions());
  }

  @DisplayName("반복 테스트")
  @RepeatedTest(value = 10, name = "{displayName}, {currentRepetition}/{totalRepetitions}")
  void repeatTest3() {
    System.out.println("repeatTest");
  }

  @ParameterizedTest(name = "{index} {displayName} message={0}")
  @DisplayName("파라미터화 테스트")
  @ValueSource(strings = {"A", "B", "C"})
  void parameterizedTest(String message) {
    System.out.println(message);
  }

}

Junit5 CsvSource 사용하기

aggregation 관련한 참고 글

@ParameterizedTest(name = "{index} {displayName} message={0}")
@ValueSource(ints = {10, 20, 40})
void parameterizedTest(@ConvertWith(StudyConverter.class) Study study) {
    System.out.println(study.getLimit());
}

static class StudyConverter extends SimpleArgumentConverter{
    @Override
    protected Object convert(Object o, Class<?> aClass) throws ArgumentConversionException {
        assertEquals(Study.class, aClass, "Can only convert to Study");
        return new Study(Integer.parseInt(o.toString()));
    }
}
@ParameterizedTest(name = "{index} {displayName} message={0}")
@CsvSource({"10, '자바 스터디'", "20, 스프링"})
void parameterizedTest2(ArgumentsAccessor argumentsAccessor) {
    Study study = new Study(argumentsAccessor.getInteger(0), argumentsAccessor.getString(1));
    System.out.println(study);
}
@ParameterizedTest(name = "{index} {displayName} message={0}")
@CsvSource({"10, '자바 스터디'", "20, 스프링"})
void parameterizedTest3(@AggregateWith(StudyAggregator.class) Study study) {
    System.out.println(study);
}

static class StudyAggregator implements ArgumentsAggregator {
    @Override
    public Object aggregateArguments(ArgumentsAccessor argumentsAccessor, ParameterContext parameterContext) throws ArgumentsAggregationException {
        return new Study(argumentsAccessor.getInteger(0), argumentsAccessor.getString(1));
    }
}

댓글남기기