@ParameterizedTest 를 사용하면 하나의 테스트 메소드로 여러 개의 파라미터에 대해서 테스트 가능
testImplementation 'org.junit.jupiter:junit-jupiter-params:5.4.2'
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>5.4.2</version>
<scope>test</scope>
</dependency>
@ParameterizedTest
@ValueSource(strings = {"", " ", "a", "abc"})
void testWithValueSource(String input) {
assertTrue(Strings.isBlank(input));
}
@ParameterizedTest
@ValueSource(ints = { 1, 2, 3 })
void testWithValueSource(int argument) {
assertTrue(argument > 0 && argument < 4);
}
@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());
}
@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);
}
@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, "", " ");
}
}