I cannot figure out if it is a missing feature, see the JUnit issue 1154 and my comment there, or just my inability to write properly the syntax for name of the @ParameterizedTest
in JUnit5. Given the fact that issue has been open for last 3 years, I'm afraid I cannot wait till I get answer or even the implementation there, so I'm trying to ask here as well.
In my example ParameterizedTest, I'm using @MethodSource
, which returns a Stream of ints arrays and I would like to use only those array's attributes instead of all {arguments}
which is anyway same if I use {0}
. Hard to explain verbally here, better to use code example bellow.
package net.delphym.unittests;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import java.util.stream.Stream;
import static org.junit.jupiter.api.Assertions.assertEquals;
class DynamicTestCreationTestParam {
private static Stream<int[]> data() {
return Stream.of(new int[][] { {1, 2, 2}, {5, 3, 15}, {121, 4, 484} });
}
@DisplayName("Multiplication test")
@ParameterizedTest(name = "#{index} for {arguments}: {0} x {1} = {2}")
@MethodSource("data")
void testWithStringParameter(int[] data) {
MyClass tester = new MyClass();
int m1 = data[0];
int m2 = data[1];
int expected = data[2];
assertEquals(expected, tester.multiply(m1, m2));
}
// class to be tested
class MyClass {
public int multiply(int i, int j) {
return i *j;
}
}
}
The test results output looks something like this for the 1st run: #1 for [1, 2, 2]: [1, 2, 2] x {1} = {2}
Ideally, I would like to see this: #1 multiply: 1 x 2 = 2
.
So question is what would be the @ParametrizedTest(
name)
definition to match my expectation?
I was trying something like this:
name = "#{index} multiply : {[0][0]} x {[0][1]} = {[0][2]}"
But it is syntacticly incorrect.
Alright, as suggested by @Murat Karagöz, I'm only reposting suggestions which I receive on Github to have all at one place.
In my opinion, without no doubt, those elegant solutions are only workarounds to the JUnit5 @ParameterizedTest API limitations, but they both satisfied my needs.
Proposed workaround #1 if I don't need to use
int[]
Solution presented here is preserving
@MethodSource("data")
and only altering its return type. Now thedata()
method returns Stream of Arguments objects (3 in each), instead Stream of int arrays (of 3 in each).Then the code reads:
Credits goes to Marc Philipp for his post @ Github.
Proposed workaround #2 to unroll test data to match display needs##
This solution provided by sormarus suggest to use
@CsvSource
instead of the@MethodSource
and provides the set of the test data within the annotation.I like this as simple example how to quickly prepare same basic variation without a need to have a specific data generating method.
The code looks like this: