java - 如何编写带有两个带有过滤值的参数的@ParametrizedTest?
问题描述
考虑我的测试类:
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
. 使用例如 a 来实现这一目标的最佳方法是@MethodSource
什么?还是有更多类似junit的方式来实现类似的结果?如果参数空间更高维,那将是什么方法呢?
解决方案
是的,它确实适用于@MethodSource
using lambdas:
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();
}
对于更高的维度,嵌套更多.forEach
的就足够了
推荐阅读
- spring - 是否可以在 k8s configMaps 中进行参数替换?
- python-3.x - 如何从 np.where() 而不是 True/False 获取单元格的值
- r - ggplot2 图例标题不尊重 Google 字体的粗体
- reactjs - 如何在 Ant Design Form 中使用 async onFinish 方法
- firebase - Firebase SwiftUI and Firebase Auth - not reading user ID?
- gradle - Gradle 如何自动更新依赖的版本?
- scala - Spark - 从数组对象中选择多列
- debugging - 通过 UART 将缓冲区作为 USB 数据发送到 VCP 时无法访问内存位置
- python - 为什么硬编码的 url 有效,但 Django 中的名称空间 url 无效?
- java - 如何获取可启动应用程序的列表?