https://junit.org/junit5/docs/current/user-guide/#overview

Parameterized Tests

several cases for testing with at least one source to provide the arguments.

ValueSource

1
2
3
4
5
6
@ParameterizedTest
@ValueSource(strings = {"racecar", "radar", "able was I ere I saw elba"})
void palindromes(String candidate) {
assertTrue(StringUtils.isPalindrome(candidate));
}

EnumSource

1
2
3
4
5
6
@ParameterizedTest
@EnumSource(value = Person.Gender.class)
void testWithEnumSource(Person.Gender gender) {
assertTrue(Person.Gender.F == gender || Person.Gender.M == gender);
}

MethodSource

1
2
3
4
5
6
7
8
9
10
11
12
13
14
@ParameterizedTest
@MethodSource("stringIntAndListProvider")
void testWithMultiArgMethodSource(String str, int num, List<String> list) {
assertEquals(5, str.length());
assertTrue(num >= 1 && num <= 2);
assertEquals(2, list.size());
}

static Stream<Arguments> stringIntAndListProvider() {
return Stream.of(
arguments("apple", 1, Arrays.asList("a", "b")),
arguments("lemon", 2, Arrays.asList("x", "y"))
);
}

CsvSource

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
@ParameterizedTest
@CsvSource({
"Jane, Doe, F, 1990-05-20",
"John, Doe, M, 1990-10-22"
})
void testWithArgumentsAccessor(ArgumentsAccessor arguments) {
Person person = new Person(arguments.getString(0),
arguments.getString(1),
arguments.get(2, Person.Gender.class),
arguments.get(3, LocalDate.class));

if (person.getFirstName().equals("Jane")) {
assertEquals(Person.Gender.F, person.getGender());
} else {
assertEquals(Person.Gender.M, person.getGender());
}
assertEquals("Doe", person.getLastName());
assertEquals(1990, person.getDateOfBirth().getYear());
}

CsvFileSource

1
2
3
4
5
6
7
8
9
10
11
12
13
@ParameterizedTest
@CsvFileSource(resources = "/two-column.csv", numLinesToSkip = 1)
void testWithCsvFileSourceFromClasspath(String country, int reference) {
assertNotNull(country);
assertNotEquals(0, reference);
}

// content in resources/two-column.csv
// COUNTRY, REFERENCE
// Sweden, 1
// Poland, 2
// "United States of America", 3
// France, 700_000

ArgumentsSource

1
2
3
4
5
6
7
8
9
10
11
12
13
@ParameterizedTest
@ArgumentsSource(MyArgumentsProvider.class)
void testWithArgumentsSource(String argument) {
assertNotNull(argument);
}

static class MyArgumentsProvider implements ArgumentsProvider {

@Override
public Stream<? extends Arguments> provideArguments(ExtensionContext context) {
return Stream.of("apple", "banana").map(Arguments::of);
}
}

Custom Aggregator

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
    @ParameterizedTest
@CsvSource({
"Jane, Doe, F, 1990-05-20",
"John, Doe, M, 1990-10-22"
})
void testWithCustomAggregatorAnnotation(@CsvToPerson Person person) {
if (person.getFirstName().equals("Jane")) {
assertEquals(Person.Gender.F, person.getGender());
} else {
assertEquals(Person.Gender.M, person.getGender());
}
assertEquals("Doe", person.getLastName());
assertEquals(1990, person.getDateOfBirth().getYear());
}

// CsvToPerson
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.PARAMETER)
@AggregateWith(PersonAggregator.class)
public @interface CsvToPerson {
}

// PersonAggregator
public class PersonAggregator implements ArgumentsAggregator {
@Override
public Person aggregateArguments(ArgumentsAccessor arguments, ParameterContext context) {
return new Person(arguments.getString(0),
arguments.getString(1),
arguments.get(2, Person.Gender.class),
arguments.get(3, LocalDate.class));
}
}