Reputation: 19918
PROBLEM: The same test method is used for different data. Depending on input data, test method body should expect 'equals' or 'not equal' in asserts.
RESEARCH: @ParameterizedTest
allows using @CsvSource
as test method input data:
@ParameterizedTest
@CsvSource({ "foo, 1", "bar, 2", "'baz, qux', 3" })
void testWithCsvSource(String first, int second) {
assertNotNull(first);
assertNotEquals(0, second);
}
This could be achieved by two opposite test methods, but their code would be much similar.
QUESTION: How to identify in the method body, which set of CsvSource input parameters is currently used?
Upvotes: 2
Views: 216
Reputation: 19918
An optimal solution so far is adding index as first parameter for each set:
@ParameterizedTest
@CsvSource({
"0, foo, 11", // Expected to be valid.
"1, bar, 22", // Expected to be invalid.
"2, 'baz, qux', 33" }) // Expected to be invalid.
void testWithCsvSource(
int parametersIndex,
String first,
int second) {
Assertions.assertEquals(
second.validate(),
parametersIndex == 0); // Expected true for 0 index parameters only.
}
Upvotes: 0
Reputation: 24560
You can create two test methods with different CsvSource
and then delegate to a common test method.
@ParameterizedTest
@CsvSource({ "foo, 1", "bar, 2", "'baz, qux', 3" })
void testWithCsvSource(String first, int second) {
internalTest(first, second);
}
@ParameterizedTest
@CsvSource({ "foo2, 1", "bar2, 2", "'baz2, qux2', 3" })
void testWithCsvSource2(String first, int second) {
internalTest(first, second);
}
private void internalTest(String first, int second) {
assertNotNull(first);
assertNotEquals(0, second);
}
Upvotes: 2