For example, I have few enums in my project:
- Figure, with values TRIANGLE and SQUARE
- Color, with values are RED and YELLOW
How to create a test, using the cartesian product of all of the combinations? The follow code doesn't work:
// this doesn't work
@ParameterizedTest
@EnumSource(value = Figure.class)
@EnumSource(value = Color.class)
void test(Figure figure, Color color) {
System.out.println(String.format("%s_%s",figure,color));
}
I want to get all of combinations:
TRIANGLE RED
TRIANGLE YELLOW
SQUARE RED
SQUARE YELLOW
My temporary solution is using annotation @MethodSource
// this works
@ParameterizedTest
@MethodSource("generateCartesianProduct")
void test(Figure figure, Color color) {
System.out.println(String.format("%s_%s",figure,color));
}
private static Stream<Arguments> generateCartesianProduct() {
List<Arguments> argumentsList = new ArrayList<>();
for(Figure figure : Figure.values()) {
for(Color color : Color.values()) {
argumentsList.add(Arguments.of(figure,color));
}
}
return argumentsList.stream();
}
But I don't want to have extra code in my tests. Has JUnit 5 any solution for my problem?