Using JUnit4 to executing multiple tests with different test cases

The Parameterized feature in JUnit 4 allows you run execute the same test with multiple test cases.

But what if we want to execute different tests, each with a different set of test cases?

We can use the @Parameters annotation to run through a set of index values and use the index value in the class constructor to access the test case values that correspond to the index and test number. The @After annotation can be used to advance the test number.

The limitation is that each test has to have the same number of test cases.

This simple bit of JUnit 4 code illustrates the idea.

 

import java.util.Arrays;import java.util.Collection; import static org.junit.Assert.*;
import org.junit.Test;
import org.junit.After;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import org.junit.runner.RunWith;
 

@RunWith(Parameterized.class)
public class DifferentTests
{
    private String data;
    private String expectedResult;

    static int testNum = 1;

    static Object [][] testdata = new Object[][]
        {
            {0},{1},{2},
        };

    static Object [][][] datavalues = new Object[][][]
        {
            {
                { "A1","A1"},
                { "B1","B1"},
                { "C1","C1"}
            },
            {
                { "A2","A2"},
                { "B2","B2"},
                { "C2","C2"},
            },
            {
                { "A3","A3"},
                { "B3","B3"},
                { "C3","C3"},
            },
            {
                { "A4","A4"},
                { "B4","B4"},
                { "C4","C4"},
            }
        };

 

    public DifferentTests(int index)
    {
        this.data = (String) datavalues[testNum-1][index][0];
        this.expectedResult = (String) datavalues[testNum-1][index][1];
    }

    @Parameters
    public static Collection<Object[]> generateData()
    {
        return Arrays.asList (testdata);
    }

    @After public void nextTest ()
    {
        if (++testNum > datavalues.length)
            testNum = 1;

        return;
    }

    @Test
    public void testOne()
    {
        String actualResult = this.data;
        System.out.println("T" + testNum +":data = " + actualResult);
        assertEquals(actualResult, this.expectedResult);
    }

    @Test
    public void testTwo()
    {
        String actualResult = this.data;
        System.out.println("T" + testNum +":data = " + actualResult);
        assertEquals(actualResult, this.expectedResult);
    }

    @Test
    public void testThree()
    {
        String actualResult = this.data;
        System.out.println("T" + testNum +":data = " + actualResult);
        assertEquals(actualResult, this.expectedResult);
    }

    @Test
    public void testFour()
    {
        String actualResult = this.data;
        System.out.println("T" + testNum +":data = " + actualResult);
        assertEquals(actualResult, this.expectedResult);
    }
}
 

When executed we get:

java org.junit.runner.JUnitCore DifferentTests


JUnit version 4.9b2
.T1:data = A1
.T2:data = A2
.T3:data = A3
.T4:data = A4
.T1:data = B1
.T2:data = B2
.T3:data = B3
.T4:data = B4
.T1:data = C1
.T2:data = C2
.T3:data = C3
.T4:data = C4

Time: 0.029

OK (12 tests)