How to rewrite data-driven test suites of JUnit 3 in Junit 4?

Posted by rics on Stack Overflow See other posts from Stack Overflow or by rics
Published on 2010-05-12T10:49:14Z Indexed on 2010/05/12 10:54 UTC
Read the original article Hit count: 197

Filed under:
|
|

I am using data-driven test suites running JUnit 3 based on Rainsberger's JUnit Recipes. The purpose of these tests is to check whether a certain function is properly implemented related to a set of input-output pairs.

Here is the definition of the test suite:

public static Test suite() throws Exception {
    TestSuite suite = new TestSuite();
    Calendar calendar = GregorianCalendar.getInstance();
    calendar.set(2009, 8, 05, 13, 23); // 2009. 09. 05. 13:23
    java.sql.Date date = new java.sql.Date(calendar.getTime().getTime());
    suite.addTest(new DateFormatTestToString(date, JtDateFormat.FormatType.YYYY_MON_DD, "2009-SEP-05"));
    suite.addTest(new DateFormatTestToString(date, JtDateFormat.FormatType.DD_MON_YYYY, "05/SEP/2009"));
    return suite;
}   

and the definition of the testing class:

public class DateFormatTestToString extends TestCase {

    private java.sql.Date date;
    private JtDateFormat.FormatType dateFormat;
    private String expectedStringFormat;

    public DateFormatTestToString(java.sql.Date date, JtDateFormat.FormatType dateFormat, String expectedStringFormat) {
        super("testGetString");
        this.date = date;
        this.dateFormat = dateFormat;
        this.expectedStringFormat = expectedStringFormat;
    }

    public void testGetString() {
        String result = JtDateFormat.getString(date, dateFormat);
        assertTrue( expectedStringFormat.equalsIgnoreCase(result));
    }
}

How is it possible to test several input-output parameters of a method using JUnit 4?

This question and the answers explained to me the distinction between JUnit 3 and 4 in this regard. This question and the answers describe the way to create test suite for a set of class but not for a method with a set of different parameters.

© Stack Overflow or respective owner

Related posts about unit-testing

Related posts about junit