Loading...

Parameterized Tests with JUnit

JUnit 4 has introduced a new feature: Parameterized tests. Parameterized tests allow developer to run the same test over and over again using different values. There are five steps, to create parameterized tests.

  • Annotate test class with @RunWith(Parameterized.class)
  • Create a public static method annotated with @Parameters that returns a Collection of Objects (as Array) as test data set.
  • Create a public constructor that takes in what is equivalent to one “row” of test data.
  • Create an instance variable for each “column” of test data.
  • Create your tests case(s) using the instance variables as the source of the test data.

The test case will be invoked once per each row of data. In this post, we have an implementation of a counter string generator.

This parameterized JUnit test covers random chosen numbers.

@RunWith(Parameterized.class)
public class TestStringUtilTest {
    private static final Logger LOGGER = LoggerFactory.getLogger(TestStringUtilTest.class);
    private final int length;
    private final String expectedResult;
    public TestStringUtilTest(Integer length,
            String expectedResult) {
        this.length = length;
        this.expectedResult = expectedResult;
    }
    /**
     * Input test data, numbers chosen by random.org
     *
     * @return test data
     */
    @Parameterized.Parameters
    public static Collection counterStrings() {
        return Arrays.asList(new Object[][] {
                { -5, "*3*5*" },
                { 0, "" },
                { 1, "*" },
                { 35, "2*4*6*8*11*14*17*20*23*26*29*32*35*" },
                { 10, "*3*5*7*10*" },
                { 23, "2*4*6*8*11*14*17*20*23*" },
                { 32, "2*4*6*8*11*14*17*20*23*26*29*32*" },
                { 88, "*3*5*7*10*13*16*19*22*25*28*31*34*37*40*43*46*49*52*55*58*61*64*67*70*73*76*79*82*85*88*" },
                { 1024, "*3*5*7*9*12*15*18*21*24*27*30*33*36*39*42*45*48*51*54*57*60*63*66*69*72*75*78*81*84*87*90*93*96*99*103*107*111*115*119*123*127*131*135*139*143*147*151*155*159*163*167*171*175*179*183*187*191*195*199*203*207*211*215*219*223*227*231*235*239*243*247*251*255*259*263*267*271*275*279*283*287*291*295*299*303*307*311*315*319*323*327*331*335*339*343*347*351*355*359*363*367*371*375*379*383*387*391*395*399*403*407*411*415*419*423*427*431*435*439*443*447*451*455*459*463*467*471*475*479*483*487*491*495*499*503*507*511*515*519*523*527*531*535*539*543*547*551*555*559*563*567*571*575*579*583*587*591*595*599*603*607*611*615*619*623*627*631*635*639*643*647*651*655*659*663*667*671*675*679*683*687*691*695*699*703*707*711*715*719*723*727*731*735*739*743*747*751*755*759*763*767*771*775*779*783*787*791*795*799*803*807*811*815*819*823*827*831*835*839*843*847*851*855*859*863*867*871*875*879*883*887*891*895*899*903*907*911*915*919*923*927*931*935*939*943*947*951*955*959*963*967*971*975*979*983*987*991*995*999*1004*1009*1014*1019*1024*" }
        });
    }
    @Test
    public void testCreateCounterString() throws Exception {
        //arrange
        LOGGER.info("Start test for counter string length {}", this.length);
        //act
        String actual = TestStringUtil.createCounterString(this.length);
        LOGGER.info("Generated counter string: {}", actual);
        //assert
        assertEquals(String.format("generated counter string for number %d is ok", this.length), this.expectedResult, actual);
    }