SpringUnit Tutorial

Example 5

Previous Next
In Example 5, we create a similar test for a Range, now using java.util.Calendar.
package org.springunit.examples.tutorial;

import java.util.Calendar;
import org.springunit.examples.Range;
import org.springunit.framework.SpringUnitContext;
import org.springunit.framework.SpringUnitTest;

public class RangeTutorialExample5Test extends SpringUnitTest {

    public void testIsWithinBelowLower() throws Exception {
        runIsWithin();
    }
	
    public void testIsWithinAtLower() throws Exception {
        runIsWithin();
    }
	
    public void testIsWithinBetween() throws Exception {
        runIsWithin();
    }
	
    public void testIsWithinAtUpper() throws Exception {
        runIsWithin();
    }
	
    public void testIsWithinAboveUpper() throws Exception {
        runIsWithin();
    }
	
    protected void runIsWithin() throws Exception {
        Range<Calendar> subject = getObject("subject");
        Calendar item = getObject("item");
        boolean expected = getObject("expected");
        boolean actual = subject.isWithin(item);
        assertEquals(expected, actual);
    }
	
    public SpringUnitContext getRangeTutorialExample4Test() {
        return this.rangeTutorialExample5Test;
    }

    public void setRangeTutorialExample5Test(SpringUnitContext rangeTutorialExample5Test) {
        this.rangeTutorialExample5Test = rangeTutorialExample5Test;
    }
	
    private SpringUnitContext rangeTutorialExample5Test;

}
As you can see, the runIsWithin method is identical to Example 4. In fact, the code of the entire test is identical to Example 4, with the exception of the change in name of the class and the name of its corresponding SpringUnitContext bean.
<?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="rangeTutorialExample5Test" class="org.springunit.framework.SpringUnitContext">
        <property name="data">
            <map>
            
                <entry key="testIsWithinBelowLower">
                    <map>
                        <entry key="expected">
                            <value type="boolean">false</value>
                        </entry>
                        <entry key="subject">
                            <bean class="org.springunit.examples.Range">
                                <constructor-arg>
                                    <bean class="java.util.GregorianCalendar">
                                        <constructor-arg><value type="int">2006</value></constructor-arg>
                                        <constructor-arg><value type="int">5</value></constructor-arg>
                                        <constructor-arg><value type="int">31</value></constructor-arg>
                                        <constructor-arg><value type="int">20</value></constructor-arg>
                                        <constructor-arg><value type="int">30</value></constructor-arg>
                                        <constructor-arg><value type="int">40</value></constructor-arg>
                                    </bean>
                                </constructor-arg>
                                <constructor-arg>
                                    <bean class="java.util.GregorianCalendar">
                                        <constructor-arg><value type="int">2006</value></constructor-arg>
                                        <constructor-arg><value type="int">5</value></constructor-arg>
                                        <constructor-arg><value type="int">1</value></constructor-arg>
                                        <constructor-arg><value type="int">8</value></constructor-arg>
                                        <constructor-arg><value type="int">10</value></constructor-arg>
                                        <constructor-arg><value type="int">20</value></constructor-arg>
                                    </bean>
                                </constructor-arg>
                            </bean>
                        </entry>
                        <entry key="item">
                            <bean class="java.util.GregorianCalendar">
                                <constructor-arg><value type="int">2006</value></constructor-arg>
                                <constructor-arg><value type="int">5</value></constructor-arg>
                                <constructor-arg><value type="int">1</value></constructor-arg>
                                <constructor-arg><value type="int">8</value></constructor-arg>
                                <constructor-arg><value type="int">10</value></constructor-arg>
                                <constructor-arg><value type="int">19</value></constructor-arg>
                            </bean>
                        </entry>
                    </map>
                </entry>
                
                <entry key="testIsWithinAtLower">
                    <map>
                        <entry key="expected">
                            <value type="boolean">true</value>
                        </entry>
                        <entry key="subject">
                            <bean class="org.springunit.examples.Range">
                                <constructor-arg>
                                    <bean class="java.util.GregorianCalendar">
                                        <constructor-arg><value type="int">2006</value></constructor-arg>
                                        <constructor-arg><value type="int">5</value></constructor-arg>
                                        <constructor-arg><value type="int">31</value></constructor-arg>
                                        <constructor-arg><value type="int">20</value></constructor-arg>
                                        <constructor-arg><value type="int">30</value></constructor-arg>
                                        <constructor-arg><value type="int">40</value></constructor-arg>
                                    </bean>
                                </constructor-arg>
                                <constructor-arg>
                                    <bean class="java.util.GregorianCalendar">
                                        <constructor-arg><value type="int">2006</value></constructor-arg>
                                        <constructor-arg><value type="int">5</value></constructor-arg>
                                        <constructor-arg><value type="int">1</value></constructor-arg>
                                        <constructor-arg><value type="int">8</value></constructor-arg>
                                        <constructor-arg><value type="int">10</value></constructor-arg>
                                        <constructor-arg><value type="int">20</value></constructor-arg>
                                    </bean>
                                </constructor-arg>
                            </bean>
                        </entry>
                        <entry key="item">
                            <bean class="java.util.GregorianCalendar">
                                <constructor-arg><value type="int">2006</value></constructor-arg>
                                <constructor-arg><value type="int">5</value></constructor-arg>
                                <constructor-arg><value type="int">1</value></constructor-arg>
                                <constructor-arg><value type="int">8</value></constructor-arg>
                                <constructor-arg><value type="int">10</value></constructor-arg>
                                <constructor-arg><value type="int">20</value></constructor-arg>
                            </bean>
                        </entry>
                    </map>
                </entry>
                
                <!-- More test data follows -->
                
            </map>
        </property>
    </bean>
    
</beans>
Hopefully, you are uncomfortable with the amount of duplicated code that we have just introduced. After all, the point of generics is that we can apply a common algorithm to different types without having to replicate code. That principle should apply as well to the tests as the code under test. In Example 6, we will refactor these tests and the test data in order to eliminate redundancy.
Previous Next