gpt4 book ai didi

java - 参数化 JUnit+JUnitParams 测试不适用于具有

转载 作者:行者123 更新时间:2023-12-01 12:43:18 24 4
gpt4 key购买 nike

你能告诉我为什么测试不起作用吗?我得到:

java.lang.IllegalStateException: While trying to create object of class class [Ljava.lang.Integer; could not find constructor with arguments matching (type-wise) the ones given in parameters.

我找不到 JUnitParamsRunner 使用数组作为参数的示例。

@RunWith(JUnitParamsRunner.class)
public class StatisticsUtilsParameterizedTest {

private Object[] getValues() {
Object[] objects = new Object[2];
objects[0] = new Integer[]{1, 2, 3};
objects[1] = 2;
return objects;
}

@Test
@Parameters(method = "getValues")
public void shouldCalcAverageOK(Integer[] arg, int expected) {
int average = StatisticsUtils.getAverage(arg);//requires an array
assertEquals(expected, average);
}
}

有办法让它与 JUnitParams 一起工作吗?

最佳答案

试试这个:

private Object[] getValues() {
return $(
$($(1,2,3), 2),
$($(2,3,4), 4)
);
}

或者以您尝试编写的方式

  private Object[] getValues() {
Object[] objects = new Object[2];
objects[0] = new Object[]{new Integer[]{1, 2, 3},2};
objects[1] = new Object[]{new Integer[]{2, 3, 4},4};
return objects;
}

希望这有帮助。

关于java - 参数化 JUnit+JUnitParams 测试不适用于具有,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24892696/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com