I am working with JUnit 5 and I want to create parameterized tests in a nested class. For example:
class CardTest {
@Nested
class Cost {
Stream<Arguments> cards() {
return Stream.of(
Arguments.of(Card.common(0, Color.RED), 0),
/** Other Data **/
Arguments.of(Card.choseColor(), 50)
);
}
@MethodSource("cards")
@ParameterizedTest
void cardCost(Card card, int cost) {
assertThat(card.cost()).isEqualTo(cost);
}
}
/** Some other nested classes or simple methods **/
}
The problem is @MethodSource
required that specified method must be static
. But Java does not allow static methods in non-static inner classes. If I declare the class Cost static
then it won't be collected by JUnit.
What should I do to resolve this issue?