0

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?

1 Answer 1

2

Your code has two problems:

  1. You have to use the same type for your array, otherwise you will
    get IllegalArgumentException: argument type mismatch

  2. 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));
   }

}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.