SpringUnit Tutorial

Example 4

Previous Next
In Example 4, we create a test for a Range of org.springunit.examples.CompositeDateTime. The runIsWithin method retrieves a Range object, an item and an expected result, calls the isWithin method on the subject and compares the expected and actual results. Each of the five tests calls this method.
package org.springunit.examples.tutorial;

import org.springunit.examples.CompositeDateTime;
import org.springunit.examples.Range;
import org.springunit.framework.SpringUnitContext;
import org.springunit.framework.SpringUnitTest;

public class RangeTutorialExample4Test 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<CompositeDateTime> subject = getObject("subject");
        CompositeDateTime item = getObject("item");
        boolean expected = getObject("expected");
        boolean actual = subject.isWithin(item);
        assertEquals(expected, actual);
    }
	
    public SpringUnitContext getRangeTutorialExample4Test() {
        return this.rangeTutorialExample4Test;
    }

    public void setRangeTutorialExample4Test(SpringUnitContext rangeTutorialExample4Test) {
        this.rangeTutorialExample4Test = rangeTutorialExample4Test;
    }
	
    private SpringUnitContext rangeTutorialExample4Test;

}
Here is an excerpt from the accompanying Spring bean configuration file for this test.
<?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="rangeTutorialExample4Test" 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="org.springunit.examples.CompositeDateTime">
                                        <constructor-arg>
                                            <bean class="org.springunit.examples.CompositeDate">
                                                <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>
                                            </bean>
                                        </constructor-arg>
                                        <constructor-arg>
                                            <bean class="org.springunit.examples.CompositeTime">
                                                <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>
                                    </bean>
                                </constructor-arg>
                                <constructor-arg>
                                    <bean class="org.springunit.examples.CompositeDateTime">
                                        <constructor-arg>
                                            <bean class="org.springunit.examples.CompositeDate">
                                                <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>
                                            </bean>
                                        </constructor-arg>
                                        <constructor-arg>
                                            <bean class="org.springunit.examples.CompositeTime">
                                                <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>
                                </constructor-arg>
                            </bean>
                        </entry>
                        <entry key="item">
                            <bean class="org.springunit.examples.CompositeDateTime">
                                <constructor-arg>
                                    <bean class="org.springunit.examples.CompositeDate">
                                        <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>
                                    </bean>
                                </constructor-arg>
                                <constructor-arg>
                                    <bean class="org.springunit.examples.CompositeTime">
                                        <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>
                                </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="org.springunit.examples.CompositeDateTime">
                                        <constructor-arg>
                                            <bean class="org.springunit.examples.CompositeDate">
                                                <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>
                                            </bean>
                                        </constructor-arg>
                                        <constructor-arg>
                                            <bean class="org.springunit.examples.CompositeTime">
                                                <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>
                                    </bean>
                                </constructor-arg>
                                <constructor-arg>
                                    <bean class="org.springunit.examples.CompositeDateTime">
                                        <constructor-arg>
                                            <bean class="org.springunit.examples.CompositeDate">
                                                <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>
                                            </bean>
                                        </constructor-arg>
                                        <constructor-arg>
                                            <bean class="org.springunit.examples.CompositeTime">
                                                <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>
                                </constructor-arg>
                            </bean>
                        </entry>
                        <entry key="item">
                            <bean class="org.springunit.examples.CompositeDateTime">
                                <constructor-arg>
                                    <bean class="org.springunit.examples.CompositeDate">
                                        <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>
                                    </bean>
                                </constructor-arg>
                                <constructor-arg>
                                    <bean class="org.springunit.examples.CompositeTime">
                                        <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>
                    </map>
                </entry>
				
                <!-- other test data follows here -->
				
            </map>
        </property>
    </bean>
	
</beans>
Previous Next