SpringUnit Tutorial

Example 3

Previous Next
In third example, we will refactor the first two examples into a single test. Recall that in Example 1, none of the test cases threw exceptions, as opposed to Example 2, where all test cases did. If we treat exceptions like any other kind of expected data, we can unify these two algorithms into one.
package org.springunit.examples.tutorial;

import org.springunit.examples.CompositeDate;
import org.springunit.framework.SpringUnitContext;
import org.springunit.framework.SpringUnitTest;

public class CompositeDateTutorialExample3Test extends SpringUnitTest {

    public void testJan01() throws Exception {
        runSetDay();
    }
	
    public void testDec31() throws Exception {
        runSetDay();
    }

    public void testApr31() throws Exception {
        runSetDay();
    }
	
    public void testFeb29() throws Exception {
        runSetDay();
    }

    protected void runSetDay() throws Exception {
        CompositeDate subject = getObject("subject");
        Integer day = getObject("day");
        Integer expectedDay = getObject("expectedDay");
        Exception expectedException = getObject("expectedException");
        try {
            subject.setDay(day);
            if (expectedException != null) {
                fail("Exception not thrown");
            }
            assertTrue(expectedDay == subject.getDay());
        }
        catch (Exception ex) {
            if (expectedException != null && !expectedException.getClass().isAssignableFrom(ex.getClass())) {
                throw ex;
            }
        }
    }

    public SpringUnitContext getCompositeDateTutorialExample3Test() {
        return this.compositeDateTutorialExample3Test;
    }

    public void setCompositeDateTutorialExample3Test(
            SpringUnitContext compositeDateTutorialExample3Test) {
        this.compositeDateTutorialExample3Test = compositeDateTutorialExample3Test;
    }
	
    private SpringUnitContext compositeDateTutorialExample3Test;

}
In this test, the presence or absence of an expected exception guides the test logic in both the try block and the catch block. Note, as usual, that the name of the context property is the same as the name of the test class. Here is the associated Spring bean file (CompositeDateTutorialExample3Test.xml).
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>

    <bean id="compositeDateTutorialExample3Test" class="org.springunit.framework.SpringUnitContext">
        <property name="data">
            <map>
                <entry key="testJan01">
                    <map>
                        <entry key="subject">
                            <bean class="org.springunit.examples.CompositeDate">
                                <constructor-arg><value type="int">2006</value></constructor-arg>
                                <constructor-arg><value type="int">1</value></constructor-arg>
                                <constructor-arg><value type="int">15</value></constructor-arg>
                            </bean>
                        </entry>
                        <entry key="day">
                            <value type="int">1</value>
                        </entry>
                        <entry key="expectedDay">
                            <value type="int">1</value>
                        </entry>
                    </map>
                </entry>
                <entry key="testDec31">
                    <map>
                        <entry key="subject">
                            <bean class="org.springunit.examples.CompositeDate">
                                <constructor-arg><value type="int">2006</value></constructor-arg>
                                <constructor-arg><value type="int">12</value></constructor-arg>
                                <constructor-arg><value type="int">15</value></constructor-arg>
                            </bean>
                        </entry>
                        <entry key="day">
                            <value type="int">31</value>
                        </entry>
                        <entry key="expectedDay">
                            <value type="int">31</value>
                        </entry>
                    </map>
                </entry>
                <entry key="testApr31">
                    <map>
                        <entry key="subject">
                            <bean class="org.springunit.examples.CompositeDate">
                                <constructor-arg><value type="int">2006</value></constructor-arg>
                                <constructor-arg><value type="int">4</value></constructor-arg>
                                <constructor-arg><value type="int">15</value></constructor-arg>
                            </bean>
                        </entry>
                        <entry key="day">
                            <value type="int">31</value>
                        </entry>
                        <entry key="expectedException">
                            <bean class="org.springunit.examples.InvalidDateException"/>
                        </entry>
                    </map>
                </entry>
                <entry key="testFeb29">
                    <map>
                        <entry key="subject">
                            <bean class="org.springunit.examples.CompositeDate">
                                <constructor-arg><value type="int">2006</value></constructor-arg>
                                <constructor-arg><value type="int">2</value></constructor-arg>
                                <constructor-arg><value type="int">15</value></constructor-arg>
                            </bean>
                        </entry>
                        <entry key="day">
                            <value type="int">29</value>
                        </entry>
                        <entry key="expectedException">
                            <bean class="org.springunit.examples.InvalidDateException"/>
                        </entry>
                    </map>
                </entry>
            </map>
        </property>
    </bean>
	
</beans>
The only difference worth noting in the Spring bean file is the inclusion of a InvalidDateException beans in two of the tests.
Previous Next