javaunit-testingjunitjunit4junitparams

Parametrized JUnit+JUnitParams test is not working for arrays with


Can you tell me why the test is not working? I got :

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.

And I cant find an example where JUnitParamsRunner was working with arrays as params.

@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);
    }
}

There is a way to make it work with JUnitParams?


Solution

  • try this:

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

    Or in the way you tried to write

      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;
      }
    

    Hope this helps.