0%

JUnit | Parameterized test in JUnit5

In JUnit5, we can replace @Test with @ParameterizedTest, so that test cases can use different parameters and be executed several times in a loop.

@ValueSource

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
@DisplayName("this is demo 1")
@ParameterizedTest
@ValueSource(ints = { 1, 2, 3 })
public void test_Demo1(int arg) {
int expected = 2;
assertEquals(expected, arg, "You are not such 2");
}

@DisplayName("this is demo 2")
@ParameterizedTest(name = "{index} ==> the testcase is running")
@ValueSource(strings = {"1", "2", "3,4"})
public void test_Demo2(String arg) {
assertNull(arg, "Can NOT be NULL");

String expected = "2";
assertEquals(expected, arg, "You are not such 2");
}

In @ValueSource, the parameter name can be strings, ints, etc. However, I personally prefer to use strings, because the String type can be implicitly converted to many other types which makes passing arguments more flexible.

1
2
3
4
5
6
7
8
9
10
11
@ParameterizedTest
@ValueSource(strings = {"true", "false"})
public void test_Demo3(boolean arg) {
assertTrue(arg);
}

@ParameterizedTest
@ValueSource(strings = {"0", "1.1", "2.2"})
public void test_Demo4(BigDecimal arg) {
assertEquals(BigDecimal.ZERO, arg);
}

Which types can a String be converted to automatically?

@CsvSource

1
2
3
4
5
6
7
8
@ParameterizedTest
@CsvSource({
" 85, 体育, true",
"99.5, '语,数,外', false",
})
public void test_Demo5(BigDecimal score, String subject, boolean isWin) {

}

ArgumentsAccessor, the parameter aggregator

By ArgumentsAccessor, multiple parameters can be received at once.

1
2
3
4
5
6
7
8
9
10
@ParameterizedTest
@CsvSource({
" 85, 体育, true",
"99.5, '语,数,外', false",
})
public void test_Demo6(ArgumentsAccessor args) {
args.get(0 , BigDecimal.class);
args.get(1, String.class); // OR args.getString(1);
args.get(2, Boolean.class);
}

ArgumentsAccessor doc api

@CsvFileSource

The difference with ‘@csvsource’ is reading test data from CSV file, however, the same is how to pass parameters and others.

1
2
3
4
@ParameterizedTest
@CsvFileSource(resources = "/login-data.csv", numLinesToSkip = 1)
void test_Demo7(ArgumentsAccessor args) {
}

Welcome to my other publishing channels