Consider my test 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) { ... }
}
I would like to run my test
method with the following data combinations
however, without adding further elements to fruits
or changing the signature of test
. What is the best way to achieve this using for example a @MethodSource
? Or is there any more junit-like way to achieve a similar result? And what would be the approach if the parameter space was even higher dimensional?
Yes, it works indeed with a @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();
}
For higher dimensions it's engough to nest further .forEach
s
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.