From JUnit to SpringUnit Tests

Adding Syntactic Sugar

Previous Next
The expression that pulls values from the data map is both long and frequently repeated. The new getObject method makes the test methods considerably easier to read.
    protected Object getObject(String name) {
        return this.data.get(getName()).get(name);
    }
    public void testJan01() throws Exception {
        Integer day = (Integer)getObject("day");
        Integer month = (Integer)getObject("month");
        Integer year = (Integer)getObject("year");
        Integer expectedDay = (Integer)getObject("expectedDay");
        Integer expectedMonth = (Integer)getObject("expectedMonth");
        Integer expectedYear = (Integer)getObject("expectedYear");
        runValid(year, month, day, expectedYear, expectedMonth, expectedDay);
    }

    // More valid tests here

    public void testFeb29Leap2004() throws Exception {
        Integer day = (Integer)getObject("day");
        Integer month = (Integer)getObject("month");
        Integer year = (Integer)getObject("year");
        Integer expectedDay = (Integer)getObject("expectedDay");
        Integer expectedMonth = (Integer)getObject("expectedMonth");
        Integer expectedYear = (Integer)getObject("expectedYear");
        runValid(year, month, day, expectedYear, expectedMonth, expectedDay);
    }

    public void testApr31() throws Exception {
        Integer day = (Integer)getObject("day");
        Integer month = (Integer)getObject("month");
        Integer year = (Integer)getObject("year");
        runInvalid(year, month, day);
    }

    // More invalid tests here

    public void testFeb29NoLeap() throws Exception {
        Integer day = (Integer)getObject("day");
        Integer month = (Integer)getObject("month");
        Integer year = (Integer)getObject("year");
        runInvalid(year, month, day);
    }
Not only are the tests easier to read, another refactoring (Consolidating Data Retrieval) becomes immediately evident.
Previous Next