junit5 advanced parameterized tests
Table like:
@ParameterizedTest(name = "{index} => calculates the sum of {0}: ({1}, {2})")
@CsvSource(delimiter = '|', textBlock = """
positive numbers | 10 | 6 | 16
positive and negative | -4 | 2 | -2
negative numbers | -6 | -100 | -106
""")
void calculatesSum(String description, int a, int b, int expectedSum) {
// WHEN
int actual = calculator.sum(a, b);
// THEN
Assertions.assertEquals(expectedSum, actual);
}
# prints the following in your console
calculatesSum(String, int, int) ✔
├─ 1 => calculates the sum of positive numbers: (10, 6) ✔
└─ 2 => calculates the sum of positive and negative: (-4, 2) ✔
└─ 3 => calculates the sum of negative numbers: (-6, -100) ✔
Better semantic:
@ParameterizedTest
@CsvSource(delimiterString = "maps to", textBlock = """
'foo' maps to 'bar'
'junit' maps to 'jupiter'
""")
void shouldMapPair(String input, String expected) {
String actual = pairMapper.map(input);
Assertions.assertEquals(expected, actual);
}