Case Study

Testing Finder Methods

Previous Next
Here, we demonstrate testing of finder methods by examining ProductDaoHibernateTest. ProductDaoHibernate exports a method getProductListByCategoryName that, given the name of a category, returns a list of Products whose category has that name. Here is an excerpt of the test code. ProductDaoHibernateTest.java (excerpt)
public class ProductDaoHibernateTest<V extends Product, D extends ProductDaoHibernate<V>> extends AbstractDaoTest<V, D> {

	public void testGetProductListByCategoryNameFound() throws Exception {
		runGetProductListByCategoryName();
	}
	
	public void testGetProductListByCategoryNameNotFound() throws Exception {
		runGetProductListByCategoryName();
	}
	
	protected void runGetProductListByCategoryName() throws Exception {
		String categoryName = getObject("categoryName");
		List<? extends V> expected = (List<? extends V>)getObject("expected");
		Exception expectedException = getObject("expectedException");
		try {
			List<? extends V> actual = getSubject().getProductListByCategoryName(categoryName);
			if (expectedException != null) {
				fail("Exception not thrown.");
			}
			assertTrue(areEqual(expected, actual));
		}
		catch (Exception ex) {
			if (expectedException == null || !expectedException.getClass().isAssignableFrom(ex.getClass())) {
				throw ex;
			}
		}
	}

}
There are two tests of this method, testGetProductListByCategoryNameFound and testGetProductListByCategoryNameNotFound, each of which calls runGetProductListByCategoryName. The structure of the test is easy to follow. It obtains the name of the category to search, the list of objects expected to be returned by the search, and the expected exception, if any, from the SpringUnit context. Then it compares the actual result with the expected result, checking for correct exception handling as necessary. Naturally, this test assumes that the database already contains Products that have relationships with Category objects. Those values are prepopulated by the framework, using values obtained from the SpringUnit context. Here is an excerpt from ProductDaoHibernateTest.xml, where the data values are described. ProductDaoHibernateTest.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:daoContext.xml"/>
	<import resource="ProductData.xml"/>
	
	<bean id="productDaoHibernateTest" class="org.springunit.framework.SpringUnitContext">
		<property name="data">
			<map>
				<entry key="testGetProductListByCategoryNameFound">
					<map>
						<entry key="subject">
							<ref bean="productDao"/>
						</entry>
						<entry key="prepopulated">
							<list>
								<ref bean="product1"/>
								<ref bean="product2"/>
								<ref bean="product3"/>
								<ref bean="product4"/>
								<ref bean="product5"/>
								<ref bean="product6"/>
								<ref bean="product7"/>
							</list>
						</entry>
						<entry key="categoryName">
							<value>Cat Food</value>
						</entry>
						<entry key="expected">
							<list>
								<ref bean="product1"/>
								<ref bean="product2"/>
								<ref bean="product3"/>
								<ref bean="product4"/>
							</list>
						</entry>
					</map>
				</entry>
				<entry key="testGetProductListByCategoryNameNotFound">
					<map>
						<entry key="subject">
							<ref bean="productDao"/>
						</entry>
						<entry key="prepopulated">
							<list>
								<ref bean="product1"/>
								<ref bean="product2"/>
								<ref bean="product3"/>
								<ref bean="product4"/>
								<ref bean="product5"/>
								<ref bean="product6"/>
								<ref bean="product7"/>
							</list>
						</entry>
						<entry key="categoryName">
							<value>Hamster Food</value>
						</entry>
						<entry key="expected">
							<list>
							</list>
						</entry>
					</map>
				</entry>
				<! -- other entries omitted -->
			</map>
		</property>
	</bean>
	
</beans>
Both tests prepopulate the database with seven Product objects. testGetProductListByCategoryNameFound uses "Cat Food" as search criteria, testGetProductListByCategoryNameNotFound uses "Hamster Food". The first test expects a set of four products to be returned; the second expects an empty set. By examining excerpts of the reusable data sets, ProductData.xml and CategoryData.xml, we can confirm that these expected results correspond with these search criteria. ProductData.xml (excerpt)
	<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="product4" class="org.springunit.framework.samples.jpetstore.domain.Product" singleton="false">
		<property name="category"><ref bean="category101"/></property>
		<property name="name"><value>Cat's Meow Whitefish Feast</value></property>
		<property name="description"><value>Canned whitefish</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>
CategoryData.xml (excerpt)

	<bean id="category101" class="org.springunit.framework.samples.jpetstore.domain.Category">
		<property name="name"><value>Cat Food</value></property>
		<property name="description"><value>Food for cats</value></property>
	</bean>

	<bean id="category102" class="org.springunit.framework.samples.jpetstore.domain.Category">
		<property name="name"><value>Dog Food</value></property>
		<property name="description"><value>Food for dogs</value></property>
	</bean>

	<bean id="category103" class="org.springunit.framework.samples.jpetstore.domain.Category">
		<property name="name"><value>Pet Toys</value></property>
		<property name="description"><value>Toys for all manner of pets</value></property>
	</bean>

Previous Next