Issue
Is there any way to test cases with arrays and not integers? For example:
private int[] myArray;
public SortingTestNullCase(int[] arr){
this.myArray=arr;
}
@Parameterized.Parameters
public static Collection testCases() {
return Arrays.asList(new Integer[][] {
{1,1,1},
{2,2,2}
});
}
I always get an error "java.lang.IllegalArgumentException: wrong number of arguments", because I am taking Integers in my constructor with Parameterized parameters, is there any way I can test inputs with arrays?
Solution
Your code has two problems:
You have to use the same type for your array, otherwise you will
getIllegalArgumentException: argument type mismatch
Your test data creates only one parameter input by creating only one
Integer[][]
array.
Here is some code that should work for you
@RunWith(Parameterized.class)
public class PTest {
private Integer[] myArray;
public PTest(Integer[] array) {
myArray = array;
}
@Parameters
public static Collection testCases() {
return Arrays.asList(new Integer[][] { { 1, 1, 1 } }, new Integer[][] { { 2, 2, 2 } });
}
@Test
public void doTest() {
System.out.println(Arrays.toString(myArray));
}
}
Result looks like
[1, 1, 1]
[2, 2, 2]
Instead of using a constructor you could use the @Parameter
annotation for your data member, which must be public
then
@RunWith(Parameterized.class)
public class PTest {
@Parameter
public Integer[] myArray;
@Parameters
public static Collection testCases() {
return Arrays.asList(new Integer[][] { { 1, 1, 1 } }, new Integer[][] { { 2, 2, 2 } });
}
@Test
public void doTest() {
System.out.println(Arrays.toString(myArray));
}
}
Answered By - Michael Katt
Answer Checked By - Terry (JavaFixing Volunteer)