Previous | Next |
package org.springunit.examples.tutorial; import org.springunit.examples.CompositeDate; import org.springunit.framework.SpringUnitTest; public class CompositeDateTutorialExample1Test extends SpringUnitTest { public void testJan01() throws Exception { runSetDay(); } public void testDec31() throws Exception { runSetDay(); } protected void runSetDay() throws Exception { CompositeDate subject = getObject("subject"); int day = getObject("day"); int expectedDay = getObject("expectedDay"); subject.setDay(day); assertEquals(expectedDay, subject.getDay()); } // properties still to be added }
package org.springunit.examples.tutorial; public class CompositeDateTutorialExample1Test extends SpringUnitTest { // Test methods omitted public SpringUnitContext getCompositeDateTutorialExample1Test() { return this.compositeDateTutorialExample1Test; } public void setCompositeDateTutorialExample1Test( SpringUnitContext compositeDateTutorialExample1Test) { this.compositeDateTutorialExample1Test = compositeDateTutorialExample1Test; } private SpringUnitContext compositeDateTutorialExample1Test; }
<?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="compositeDateTutorialExample1Test" class="org.springunit.framework.SpringUnitContext"> <property name="data"> <map> <!-- Test data values go here --> </map> </property> </bean> </beans>
com.foo.bar.MyTest
, then a file named
MyTest.xml
must be located in on the classpath in
com/foo/bar/
.
Earlier versions of SpringUnit expected to find this file in the
default package at the root of the classpath.
This is supported for backward-compatibility but is now discouraged.
A SpringUnitContext is a bean with one property, called "data".
This property is a map that holds all of the test data values as shown next.
<?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="compositeDateTutorialExample1Test" 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> </map> </property> </bean> </beans>
The map named "data" itself contains maps, each of whose key is the name of a test. This leads to the fourth convention: for every test method in the test class, there exists an entry in the data map whose key is the name of the test and whose value is a map. (Later, in Example 6, we will see that the data map can contain an element whose is are not the name as the name of a test.) So far, our test has two methods, so the data map contains two entries, identified by "testJan01" and "testDec31". Notice that each test forms something of a namespace, so that the same object name (e.g. "day") refers to a distinct object as each test is executed.
Now it is plain how our two boundary cases for the setDay method will be tested. In the first test, we start with the date of January 15 and change it to January 1; in the second, we begin with December 15 and change to December 31. Again, making sure that the Spring bean XML file has been correctly named and can be found on the classpath, you should be able to execute this test and see it pass.
Previous | Next |