Search code examples
javajunit5junit-jupiter

How can I insert and get data from a collection of java objects to be able to create a parameterized method?


The form "inputConditionsTest[0]" is incorrect.How can I get the first values of my collection <Object[]>???

public class ControllaCondizioniParamTest {

    private  ControllaCondizioni controllaCondizioni;

    public static Collection<Object[]> inputConditionsTest() {
        return Arrays.asList(new Object[][] {
                { 5, 3, true, true, true },
                { 1, 2, false, true, true },
                { 3, 3, false, false, true },
                { 2, 4, true, false, true}, 
                { 9, 3, false, true, false},
                { 10, 5, true, false, true }
        });
    }
        @ParameterizedTest
    @MethodSource(value = "inputConditionsTest")
    void testConditionsMethod(Collection<Object[]> inputConditionsTest) {
        controllaCondizioni = new ControllaCondizioni();
        int a = inputConditionsTest[0]
        int b = inputConditionsTest[1];
        boolean c = inputConditionsTest[2];
        boolean d = inputConditionsTest[3];
        boolean expected = inputConditionsTest[4];
        assertEquals(expected, controllaCondizioni.Verifica(a, b, c, expected));
    }

}


Solution

  • You are using JUnit's Parameterized incorrectly. The parameter source method should return a collection of arrays, you do that correctly. But the test method should receive the array elements as separate arguments:

        @ParameterizedTest
        @MethodSource(value = "inputConditionsTest")
        void testConditionsMethod(int a, int b, boolean c, boolean d, boolean expected) {
            controllaCondizioni = new ControllaCondizioni();
            assertEquals(expected, controllaCondizioni.Verifica(a, b, c, expected));
        }