@Parameterized Tests

HoJeong Choi·2023년 3월 21일
0

@ParameterizedTest 를 사용하면 하나의 테스트 메소드로 여러 개의 파라미터에 대해서 테스트 가능

의존성 추가

Gradle

testImplementation 'org.junit.jupiter:junit-jupiter-params:5.4.2'

Maven

<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-params</artifactId>
    <version>5.4.2</version>
    <scope>test</scope>
</dependency>

@ParameterizedTest

@ValueSource

  • test 메소드 실행 당 하나의 인수(argument)만 전달할때 사용 가능
  • 리터럴 값의 단일 배열을 지정
    * short, byte, int, long, float, double, char, java.lang.String, java.lang.Class
  • String
   @ParameterizedTest
   @ValueSource(strings = {"", "  ", "a", "abc"})
  void testWithValueSource(String input) {
      assertTrue(Strings.isBlank(input));
  }
  • int
	@ParameterizedTest
	@ValueSource(ints = { 1, 2, 3 })
	void testWithValueSource(int argument) {
     assertTrue(argument > 0 && argument < 4);
	}

Null and Empty Sources

  • @NullSource : 파라미터 값으로 null
  • @EmptySource : 파라미터 값으로 empty
  • @NullAndEmptySource : 파라미터 값으로 null과 empty를 함께 제공
    * @ValueSource 함께 사용 가능
@ParameterizedTest
@NullSource
@EmptySource
void nullEmptyStrings(String text) {
    assertTrue(text == null || text.trim().isEmpty());
}
@ParameterizedTest
@NullAndEmptySource
@ValueSource(strings = { " ", "   ", "\t", "\n" })
void nullEmptyAndBlankStrings(String text) {
    assertTrue(text == null || text.trim().isEmpty());
}

@EnumSource

  • 파라미터로 enum 사용
@ParameterizedTest
@EnumSource(value = TimeUnit.class, names = { "DAYS", "HOURS" })
void testWithEnumSourceInclude(TimeUnit timeUnit) {
    assertTrue(EnumSet.of(TimeUnit.DAYS, TimeUnit.HOURS).contains(timeUnit));
}
@EnumSource(BoardType.class)
  void testWithEnumSource(BoardType boardType) {
    assertTrue(board.getRecentList(boardType)).isInstanceOf(
    Boards.class);
  }

@MethodSource

  • 복잡한 인수를 제공하는 방법
  • @MethodSource 에 설정하는 이름은 존재하는 메서드 이름이어야 한다.
    * 이름을 설정하지 않으면 JUnit은 test method와 이름이 같은 source method를 찾는다.
  • @MethodSource를 이용하면 다른 테스트 클래스 간에 인수를 공유하는 것도 가능
    * 이 경우 정규화된 이름 (FQN#methodName format)으로 현재 클래스 외부의 source method 를 참조할 수 있다.
 @ParameterizedTest
  @MethodSource("provideStringsForIsBlank") // needs to match an existing method.
  void isBlank_ShouldReturnTrueForNullOrBlankStrings(String input, boolean expected) {
      assertEquals(expected, Strings.isBlank(input));
  }
  
  private static Stream<Arguments> provideStringsForIsBlank() { 
      return Stream.of(
        Arguments.of(null, true),
        Arguments.of("", true),
        Arguments.of("  ", true),
        Arguments.of("not blank", false)
      );
  }
  
  //동일한 파라미터값으로 200번 반복
  private static Stream<Arguments> provideStringsForIsBlank() { 
      return Stream.generate(() -> Arguments.of("not blank", false)).limit(200);
  }
  ....
  
  
  ```java
  class StringsUnitTest {
  	// 클래스 외부의 source method
      @ParameterizedTest
      @MethodSource("com.baeldung.parameterized.StringParams#blankStrings") 
      void isBlank_ShouldReturnTrueForNullOrBlankStringsExternalSource(String input) {
          assertTrue(Strings.isBlank(input));
      }
  }
----  
   public class StringParams {
      static Stream<String> blankStrings() {
          return Stream.of(null, "", "  ");
      }
  }
  

0개의 댓글