Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loop through array, each element a JUnit test

I have a JUnit 4 test that loops through an array of test data:

public @Test void testAll() {

    final Object[][] sets = new Object[][] {
            // SET                              TYPE VALUE

            // --- valid sets

            // groups
            x(s(A,1, B,1, C,1),                 G),
            x(s(A,4, B,4, D,4),                 G),
            x(s(A,8, B,8, D,8, C,8),            G),
            x(s(J,J, B,4, D,4),                 G,  4*3),
            x(s(A,9, J,J, D,9),                 G,  9*3),
            x(s(A,2, B,2, C,2),                 G),
            x(s(A,4, B,4, J,J),                 G,  4*3),
            x(s(A,4, B,4, C,4, D,4),            G),

            // runs
            x(s(A,1, A,2, A,3),                 R),
            x(s(B,8, B,9, B,10),                R),
            x(s(J,J, C,2, C,3),                 R,  6),
            x(s(D,8, D,9, J,J, D,11),           R,  38),
            x(s(D,8, D,9, J,J, J,J),            R,  38),

            // sames
            x(s(A,1, A,1),                      S),
            x(s(B,4, B,4, B,4),                 S),
            x(s(C,8, C,8),                      S),
            x(s(D,3, D,3),                      S),

            // doubt-cases, assume group (TODO: verify this is correct)
            x(s(J,J, J,J, D,4),                 G,  4*3),
            x(s(A,7, J,J, J,J),                 G,  7*3),
            x(s(J,J, D,9, J,J),                 G,  9*3),
            x(s(J,J, J,J, J,J),                 G,  1),

            // --- invalid sets
            x(s(B,1, A,2, A,3),                 I), // not same colour
            x(s(D,11, D,12, J,J, J,J),          I), // last joker is 14
            x(s(B,1, B,1, A,1),                 I), // duplicate B1
            x(s(A,1, A,2, A,3, A,5),            I), // gap A4
            x(s(J,J, A,1, J,J, B,1, C,1),       I), // one J replaces D1, then nothing left to replace
            x(s(A,1, A,2),                      I), // short
            x(s(B,1),                           I), // shorter
            x(s(A,5, A,6),                      I), // short
    };

    for (Object[] o : sets) {

        TileSet s = (TileSet) o[0];
        Type t = (Type) o[1];
        int v = (Integer) o[2];

        System.out.println(s);

        assertEquals(t, s.getType());
        assertEquals(v, s.getValue());

        // test isValid, though it's Too Simple To Break(R)
        if (t == Type.INVALID) assertFalse(s.isValid());
        else assertTrue(s.isValid());
    }

}

Because it's all in one test method, the whole test stops as soon as one element in the array fails. Is there a way around that, without making a method for each test item? Maybe something with reflection?

like image 977
Bart van Heukelom Avatar asked Nov 22 '10 16:11

Bart van Heukelom


People also ask

What is the syntax to loop through array?

The forEach method is also used to loop through arrays, but it uses a function differently than the classic "for loop". The forEach method passes a callback function for each element of an array together with the following parameters: Current Value (required) - The value of the current array element.

What does @after do in JUnit?

org.junit Annotating a public void method with @After causes that method to be run after the Test method. All @After methods are guaranteed to run even if a Before or Test method throws an exception.

Do JUnit tests run sequentially?

Basically, JUnit doesn't support ordering and the tests should be able to be run in any order. If you really need to have dependencies between tests, use TestNG, where you can have dependencies.


2 Answers

Use JUnit 4's parameterized tests. They are a perfect fit for this type of problem, although the documentation is quite lacking.

Here are a few other samples on how to use them.:

  • http://ourcraft.wordpress.com/2008/08/27/writing-a-parameterized-junit-test/
  • http://isagoksu.com/2009/development/agile-development/test-driven-development/using-junit-parameterized-annotation/
  • http://www.mkyong.com/unittest/junit-4-tutorial-6-parameterized-test/
like image 122
matt b Avatar answered Oct 18 '22 13:10

matt b


catch AssertionError and add the caught error to the errors list, at the end check the list to be empty raise a compound AssertionError if not.

like image 25
khachik Avatar answered Oct 18 '22 14:10

khachik