如何编写带有两个带过滤值的参数的@ParametrizedTest?

How to write a @ParametrizedTest with two parameters with filtered values?

考虑我的测试class:

public class TestClass {

    static public class Vegetable {
         String name

         public Vegetable(String name) { ... }
    }

    static public class Fruit {
        String name;
        List<Vegetable> assignedVegs;

        public Fruit(String name, List<Vegetable> vegs) { ... }
    }

    List<Fruit> fruits = asList(
        new Fruit("Orange", asList(new Vegetable("Potato"))),
        new Fruit("Apple", asList(new Vegetable("Potato"), new Vegetable("Carot")))
    );         

    @ParametrizedTest
    public void test(Fruit f, Vegetable v) { ... }
}

我想 运行 我的 test 方法具有以下数据组合

但是,没有向 fruits 添加更多元素或更改 test 的签名。使用 @MethodSource 实现此目的的最佳方法是什么?还是有更多类似 junit 的方法来实现类似的结果?如果参数 space 的维数更高,该方法是什么?

是的,它确实适用于使用 lambdas 的 @MethodSource

private static Stream<Arguments> testDataProvider() {
    List<Arguments> testCases = new ArrayList<>();

    fruits.forEach(fruit -> {
        fruit.assignedVegs.forEach(veg -> {
            testCases.add(Arguments.of(fruit, veg));
        });
    });

    return testCases.stream();
}

对于更高的维度,进一步嵌套就足够了 .forEachs