SpringUnit Tutorial

Example 2

Previous Next
In our second example, we will write a SpringUnit test that shows the behavior of the setDay method when called with a value that causes an exception to be thrown. In the first case, we will create a CompositeDate for a day in the middle of April and then set the day to the 31st. In the second case, we will create a date in February of a non-leap year, and set the day to the 29th. Here is the complete code of the test.
package org.springunit.examples.tutorial;

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

public class CompositeDateTutorialExample2Test extends SpringUnitTest {

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

    protected void runSetDayInvalidDate() throws Exception {
        CompositeDate subject = getObject("subject");
        int day = getObject("day");
        try {
            subject.setDay(day);
            fail("Exception not thrown");
        }
        catch (InvalidDateException ex) {
        }
    }
	
    public SpringUnitContext getCompositeDateTutorialExample2Test() {
        return this.compositeDateTutorialExample2Test;
    }

    public void setCompositeDateTutorialExample2Test(
            SpringUnitContext compositeDateTutorialExample2Test) {
        this.compositeDateTutorialExample2Test = compositeDateTutorialExample2Test;
    }
	
    private SpringUnitContext compositeDateTutorialExample2Test;

}
The two tests, testApr31 and testFeb29, each call a common method that implements the test algorithm, runSetDayInvalidDate. Note how this test follows the pattern of succeeding when the expected exception is thrown, and failing when it is not. The test contains a property of type SpringUnitContext, which as we have seen, has a name identical to that of the test class. The associated Spring bean file (named CompositeDateTutorialExample2Test.xml) is similar to the file we created in Example 1.
<?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="compositeDateTutorialExample2Test" class="org.springunit.framework.SpringUnitContext">
        <property name="data">
            <map>
                <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>
                    </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>
                     </map>
                </entry>
            </map>
        </property>
    </bean>
	
</beans>
Again, the name of the outermost bean is the same as the corresponding property on the test class. The structure of the file is familiar: each test name is the key associated with a map that holds data values used for the test.
Previous Next