我正在使用junit5,并且想在嵌套类中创建参数化测试。例如:
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 **/
}
Run Code Online (Sandbox Code Playgroud)
问题是@MethodSource要求指定的方法必须为static。但是Java不允许在非静态内部类中使用静态方法。如果我创建了Cost 类static,则不会由收集junit。
我应该怎么做才能解决这个问题?