Previous | Next |
package org.springunit.examples.tutorial; import org.springunit.examples.Range; import org.springunit.framework.SpringUnitContext; import org.springunit.framework.SpringUnitTest; public abstract class RangeTutorialExample6TestAbstract<T extends Comparable<T>> 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<T> subject = getObject("subject"); T item = getObject("item"); boolean expected = getObject("expected"); boolean actual = subject.isWithin(item); assertEquals(expected, actual); } public SpringUnitContext getRangeTutorialExample6TestAbstract() { return this.rangeTutorialExample6TestAbstract; } public void setRangeTutorialExample6TestAbstract(SpringUnitContext rangeTutorialExample6TestAbstract) { this.rangeTutorialExample6TestAbstract = rangeTutorialExample6TestAbstract; } private SpringUnitContext rangeTutorialExample6TestAbstract; }
package org.springunit.examples.tutorial; import org.springunit.examples.CompositeDateTime; import org.springunit.framework.SpringUnitContext; public class RangeTutorialExample6CompositeDateTimeTest extends RangeTutorialExample6TestAbstract<CompositeDateTime> { public SpringUnitContext getRangeTutorialExample6CompositeDateTimeTest() { return rangeTutorialExample6CompositeDateTimeTest; } public void setRangeTutorialExample6CompositeDateTimeTest( SpringUnitContext rangeTutorialExample6CompositeDateTimeTest) { this.rangeTutorialExample6CompositeDateTimeTest = rangeTutorialExample6CompositeDateTimeTest; } private SpringUnitContext rangeTutorialExample6CompositeDateTimeTest; }
package org.springunit.examples.tutorial; import java.util.Calendar; import org.springunit.examples.RangeSpringUnitTestAbstract; import org.springunit.framework.SpringUnitContext; public class RangeTutorialExample6CalendarTest extends RangeSpringUnitTestAbstract<Calendar> { public SpringUnitContext getRangeTutorialExample6CalendarTest() { return rangeTutorialExample6CalendarTest; } public void setRangeTutorialExample6CalendarTest( SpringUnitContext rangeTutorialExample6CalendarTest) { this.rangeTutorialExample6CalendarTest = rangeTutorialExample6CalendarTest; } private SpringUnitContext rangeTutorialExample6CalendarTest; }
<?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="rangeTutorialExample6CalendarTest" class="org.springunit.framework.SpringUnitContext"> <property name="data"> <map> <!-- First entry omitted --> <entry key="testIsWithinBelowLower"> <map> <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="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>
<?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="rangeTutorialExample6CalendarTest" class="org.springunit.framework.SpringUnitContext"> <property name="data"> <map> <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="testIsWithinBelowLower"> <map> <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="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>
To this point, we have seen how items and subjects are configured, but what about the expected values? Given what you have just learned about SpringUnit's search algorithm, you may have guessed that these are located in the configuration file associated with the ancestor class. Here is the file for RangeTutorialExample6TestAbstract.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="rangeTutorialExample6TestAbstract" class="org.springunit.framework.SpringUnitContext"> <property name="data"> <map> <entry key="testIsWithinBelowLower"> <map> <entry key="expected"> <value type="boolean">false</value> </entry> </map> </entry> <entry key="testIsWithinAtLower"> <map> <entry key="expected"> <value type="boolean">true</value> </entry> </map> </entry> <entry key="testIsWithinBetween"> <map> <entry key="expected"> <value type="boolean">true</value> </entry> </map> </entry> <entry key="testIsWithinAtUpper"> <map> <entry key="expected"> <value type="boolean">true</value> </entry> </map> </entry> <entry key="testIsWithinAboveUpper"> <map> <entry key="expected"> <value type="boolean">false</value> </entry> </map> </entry> </map> </property> </bean> </beans>
Unlike the test data, which varies with each data type (and therefore must be partitioned into two separate tests and files) the expected results are invariant across all data types. Therefore, it is appropriate to refactor the configuration of these values into the file associated with the abstract class.
Previous | Next |