我正在尝试将二维数组传递给参数化测试。一维数组按预期工作,但 junit 在第二个提示“索引 0 处的错误解析参数”。这是不受支持还是我使用了错误的语法?
(junit 5.1.0)
// This is ok
static Stream<int[]> arrayStream1(){
return Stream.of( new int[] {1}, new int[] {2});
}
@ParameterizedTest
@MethodSource("arrayStream1")
void test1(int[] par) {
assertTrue(true);
}
// This is not
static Stream<int[][]> arrayStream2(){
return Stream.of( new int[][] {{1,2}}, new int[][] {{2,3}});
}
@ParameterizedTest
@MethodSource("arrayStream2")
void test2(int[][] par) {
assertTrue(true);
}
最佳答案
从 JUnit Jupiter 5.3.1 开始,它不受支持。
要深入了解 JUnit 团队在这方面所做的工作,请参阅相应的 GitHub issue .
但是,以下两种解决方法都可以帮助您暂时实现目标。
在对象[]中包装:
static Stream<Object[]> arrayStream3() {
return Stream.of(new Object[] { new int[][] { { 1, 2 } } }, new Object[] { new int[][] { { 2, 3 } } });
}
@ParameterizedTest
@MethodSource("arrayStream3")
void test3(int[][] par) {
System.err.println(Arrays.deepToString(par));
}
参数包装:
static Stream<Arguments> arrayStream4() {
return Stream.of(arguments((Object) new int[][] { { 1, 2 } }), arguments((Object) new int[][] { { 2, 3 } }));
}
@ParameterizedTest
@MethodSource("arrayStream4")
void test4(int[][] par) {
System.err.println(Arrays.deepToString(par));
}
关于java - 可以通过@MethodSource (junit 5.1) 传递多维数组吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53133979/