만일 테스트를 실행할 때마다 어떤 랜덤 값을 사용하거나, 테스트가 실행되는 타이밍에 따라 달라지는 조건이 있을 때 테스트를 여러번 반복적으로 실행해서 검증하는 방법도 좋은 방법입니다.

이런 경우 아주 간단하게 테스트를 반복할 수 있습니다.

@RepeatedTest

예시 코드 1

@DisplayName("반복 테스트")
@RepeatedTest(10) //...1
void repeat_test() {
    System.out.println("test");
}
  1. @RepeatedTest 의 인자로 몇 번 반복할지 설정할 수 있습니다.

예시 코드 2

@DisplayName("10번 반복 테스트 2")
@RepeatedTest(value = 10, name = "{displayName}, {currentRepetition}/{totalRepetitions}") //...1
void repeat_test_2(RepetitionInfo repetitionInfo) {
		//...2
    System.out.println("test " + repetitionInfo.getCurrentRepetition() + "/" + repetitionInfo.getTotalRepetitions());
}
  1. @RepeatedTest 의 인자로 반복 횟수와 반복 테스트 이름을 설정할 수 있습니다.

    1. value property

      • ex) value = 10
    2. name property

      • PLACEHOLDER 를 사용해서 적용할 수 있습니다.
      • ex) name = "{displayName}, {currentRepetition}/{totalRepetitions}"
        • {displayName} : @DisplayName에 적용한 이름
        • {currentRepetition} : 현재 반복 횟수
        • {totalRepetitions} : 총 반복 횟수
    3. 실행 결과

      Untitled

  2. RepetitionInfo 타입의 인자를 받을 수 있습니다.

@ParameterizedTest

테스트에 여러 다른 매개변수를 대입해가며 반복 실행합니다.

JUnit 5 에서는 기본적으로 지원하는 기능입니다. JUnit 4 에서는 서드파티 라이브러리를 사용해야 가능했던 기능입니다.

예시 코드 1