Case Study

Reusable Test Data

Previous Next
If there is a single compelling advantage in separating test data from test code, it is the opportunity for re-using test data values. SpringUnit's test data files, which are just Spring bean configuration files, can be used to define sets of data values that can be used in unit and integration tests alike. The Case Study includes files of data values for each of the object types in the domain model. Here is an excerpt from ProductData.xml followed by a UML instance diagram of this data. ProductData.xml (excerpt)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
	
	<import resource="classpath:CategoryData.xml"/>
	
	<bean id="product1" class="org.springunit.framework.samples.jpetstore.domain.Product" singleton="false">
		<property name="category"><ref bean="category101"/></property>
		<property name="name"><value>Cat's Meow Tuna Feast</value></property>
		<property name="description"><value>Canned tuna</value></property>
	</bean>

	<bean id="product2" class="org.springunit.framework.samples.jpetstore.domain.Product" singleton="false">
		<property name="category"><ref bean="category101"/></property>
		<property name="name"><value>Cat's Meow Chicken Feast</value></property>
		<property name="description"><value>Canned chicken</value></property>
	</bean>

	<bean id="product3" class="org.springunit.framework.samples.jpetstore.domain.Product" singleton="false">
		<property name="category"><ref bean="category101"/></property>
		<property name="name"><value>Cat's Meow Salmon Feast</value></property>
		<property name="description"><value>Canned salmon</value></property>
	</bean>

	<bean id="product5" class="org.springunit.framework.samples.jpetstore.domain.Product" singleton="false">
		<property name="category"><ref bean="category102"/></property>
		<property name="name"><value>Silk Purse Sow's Ears</value></property>
		<property name="description"><value>Pig's ears for chewing</value></property>
	</bean>

	<bean id="product6" class="org.springunit.framework.samples.jpetstore.domain.Product" singleton="false">
		<property name="category"><ref bean="category102"/></property>
		<property name="name"><value>Silk Purse Dog Bones</value></property>
		<property name="description"><value>Dog bones for chewing</value></property>
	</bean>

	<bean id="product7" class="org.springunit.framework.samples.jpetstore.domain.Product" singleton="false">
		<property name="category"><ref bean="category103"/></property>
		<property name="name"><value>Thumper's Bumpers</value></property>
		<property name="description"><value>Rabbit toys</value></property>
	</bean>

	<bean id="product101" class="org.springunit.framework.samples.jpetstore.domain.Product">
		<property name="category"><ref bean="category101"/></property>
		<property name="name"><value>Cat's Meow Tuna Feast</value></property>
		<property name="description"><value>Canned tuna</value></property>
	</bean>

	<bean id="product102" class="org.springunit.framework.samples.jpetstore.domain.Product">
		<property name="category"><ref bean="category101"/></property>
		<property name="name"><value>Cat's Meow Chicken Feast</value></property>
		<property name="description"><value>Canned chicken</value></property>
	</bean>

	<bean id="product103" class="org.springunit.framework.samples.jpetstore.domain.Product">
		<property name="category"><ref bean="category101"/></property>
		<property name="name"><value>Cat's Meow Salmon Feast</value></property>
		<property name="description"><value>Canned salmon</value></property>
	</bean>

	<bean id="product105" class="org.springunit.framework.samples.jpetstore.domain.Product">
		<property name="category"><ref bean="category102"/></property>
		<property name="name"><value>Silk Purse Sow's Ears</value></property>
		<property name="description"><value>Pig's ears for chewing</value></property>
	</bean>

	<bean id="product106" class="org.springunit.framework.samples.jpetstore.domain.Product">
		<property name="category"><ref bean="category102"/></property>
		<property name="name"><value>Silk Purse Dog Bones</value></property>
		<property name="description"><value>Dog bones for chewing</value></property>
	</bean>

	<bean id="product107" class="org.springunit.framework.samples.jpetstore.domain.Product">
		<property name="category"><ref bean="category103"/></property>
		<property name="name"><value>Thumper's Bumpers</value></property>
		<property name="description"><value>Rabbit toys</value></property>
	</bean>

</beans>
There are a few key points to notice. First, Product objects depend upon Category objects, which are similarly defined in a separate CategoryData.xml file. Second, notice that the data set contains versions that are singletons and non-singletons. When objects created by the Spring IOC Container need to share references to the same object, that shared object must be created in singleton scope. Here, we can see that the first three products shown are in the "Cat Food" category, and each of these is defined having a reference to a shared, singleton Category object. The product data file contains a mixture of both non-singleton and singleton objects. Non-singletons are easier to work with in JUnit, but singleton versions are needed in order to define objects having Products as dependencies.
Previous Next