我尝试运行以下代码,但收到参数数量错误错误。
package Homework7;
import static junitparams.JUnitParamsRunner.$;
import static org.junit.Assert.*;
import junitparams.JUnitParamsRunner;
import junitparams.Parameters;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@RunWith(JUnitParamsRunner.class)
public class Problem3TestClass {
private Problem3Class p3class;
@SuppressWarnings("unused")
public static final Object[] Problem3TestClass(){
return $(
// Parameters are: (1, 2)
// 1 = x,2 = y
// Test case 1
$(12223,1)
);
}
@Before
public void setUp() {
p3class = new Problem3Class();
}
@Test
@Parameters(method = "Problem3TestClass")
public void test(int[] x,int y)
{
assertEquals(y,p3class.countRepeated(x));
}
}
我的 countRepeated 方法通过以下方式调用
public int countRepeated (int[] x)
我在这里做错了什么?
最佳答案
根据source中的评论
JUnitParamsRunner#$
方法
Should not be used to create var-args arrays, because of the way Java resolves var-args for objects and primitives.
因此尝试更改测试方法以接受 List<Integer>
而不是int[]
。
下面的代码应该可以工作。
@SuppressWarnings("unused")
public static final Object[] Problem3TestClass() {
List<Integer> x = new ArrayList<Integer>();
int y = 2;
return $(x, y);
}
@Test
@Parameters(method = "Problem3TestClass")
public void test(List<Integer> x, int y) {
// update countRepeated to accept List<Integer> or do some conversion here
assertEquals(y, p3class.countRepeated(x));
}
关于java - JunitParamsRunner 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43647313/