问题描述
我正在尝试为接口Foo编写参数化测试,该接口声明了方法getFooEventInt(int,int).我编写了一个参数化测试,该测试适用于单个Foo实例(一个FooImpl对象).
I am attempting to write a parameterized test for an interface Foo, which declares a method getFooEventInt(int, int). I have written a paramterized test that works for a single instance of Foo (a FooImpl object).
public class FooTest {
@ParameterizedTest
@MethodSource("getFooEvenIntProvider")
public void getFooEvenIntTest(int seed, int expectedResult) {
Foo foo = new FooImpl();
Assertions.assertEquals(expectedResult, foo.getFooEvenInt(seed));
}
private static Stream getFooEvenIntProvider() {
return Stream.of(
Arguments.of(-2, 0),
Arguments.of(-1, 0),
Arguments.of( 0, 2),
Arguments.of( 1, 2),
);
}
}
但是,我希望能够针对提供的Foo实现实例列表调用getFooEvenIntTest(int,int),然后每次迭代都使用提供的种子/expectedResult值列表.
However, I'd like to be able to have getFooEvenIntTest(int, int) be invoked against a provided list of Foo implementation instances, with each iteration then using the provide list of seed/expectedResult values.
我意识到我可以这样做...
I realize I could do this as...
public class FooTest {
@ParameterizedTest
@MethodSource("getFooProvider")
public void getFooImplEvenIntTest(Foo foo) {
int[] expectedResult = { 0, 0, 2, 2 };
int[] seed = { -2, -1, 0, 1 };
for(int i=0; i<seed.length; i++) {
Assertions.assertEquals(expectedResult[i],
foo.getFooEvenInt(seed[i]));
}
}
private static Stream getFooProvider() {
return Stream.of(
Arguments.of(new FooImpl()),
Arguments.of(new FooImpl2())
);
}
}
有什么想法吗?如果可以解决,我会发布,但是我想我会检查一下是否可行,或者是否有其他方法.
Any ideas? I'll post if I figure it out, but I thought I'd check to see if this is even doable, or if there's a different approach.
推荐答案
我想您想到了将两个参数流组合在一起的想法.您可以通过创建两个参数列表的笛卡尔积来实现此目的.
I guess you think of combining two arguments streams. You could achieve this by creating the cartesian product of two arguments lists.
public static Stream<Arguments> cartesian(Stream a, Stream b) {
List argumentsA = (List) a.collect(Collectors.toList());
List argumentsB = (List) b.collect(Collectors.toList());
List<Arguments> result = new ArrayList();
for (Object o : argumentsA) {
Object[] objects = asArray(o);
for (Object o1 : argumentsB) {
Object[] objects1 = asArray(o1);
Object[] arguments = ArrayUtils.addAll(objects, objects1);
result.add(Arguments.of(arguments));
}
}
return result.stream();
}
private static Object[] asArray(Object o) {
Object[] objects;
if (o instanceof Arguments) {
objects = ((Arguments) o).get();
} else {
objects = new Object[]{o};
}
return objects;
}
然后您的测试代码将是:
Then your test code will be:
public static Stream<Arguments> fooIntsAndFooProvider() {
return ParameterizedTestHelper.cartesian(getFooEvenIntProvider(), getFooProvider());
}
@ParameterizedTest
@MethodSource("fooIntsAndFooProvider")
public void getFooImplEvenIntTest(Integer seed, Integer expectedResult, Foo foo) {
Assertions.assertEquals(expectedResult,
foo.getFooEvenInt(seed));
}
这篇关于是否可以嵌套JUnit 5参数化测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!