1

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?

Adam Michalik
  • 9,678
  • 13
  • 71
  • 102
Sergio
  • 3,317
  • 5
  • 32
  • 51
  • Can you really pass a reference of type Object where Integer is expected? – Anirudh Jul 22 '14 at 17:36
  • 1
    This is not intended as an answer, just a different way of looking at things. The Spock testing framework is much nicer for situations like this (actually, much nicer in general than pure JUnit) - take a look at http://spock-framework.readthedocs.org/en/latest/data_driven_testing.html#data-tables – GreyBeardedGeek Jul 22 '14 at 17:46

1 Answers1

1

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.

Sanjeev
  • 9,876
  • 2
  • 22
  • 33