Java Reference
In-Depth Information
public List findRestaurants(Address deliveryAddress,
Date deliveryTime) {
JdoTemplate jdoTemplate = getJdoTemplate();
return (List) jdoTemplate.executeFind(
new MyExampleCallback(deliveryAddress,
deliveryTime));
}
}
This version of the
findRestaurants()
method instantiates a
MyExampleCallback
,
passing the delivery address and time as constructor parameters. It then executes
the callback using
JdoTemplate
.
Because
MyExampleCallback
has an
equals()
method, it is easy to test
find-
Restaurants()
with a mock
JdoTemplate
that expects to be called with a particular
MyExampleCallback
object:
public void testFindRestaurants() {
expectedMyExampleCallback = new MyExampleCallback(…)
mockJdoTemplate.expects(once())
.method("executeFind")
.with(eq(expectedMyExampleCallback))
.will(returnValue(expectedRestaurants));
JdoTemplate jdoTemplate =
(JdoTemplate)mockJdoTemplate.proxy();
RestaurantRepository r =
new JDORestaurantRepository(jdoTemplate);
List restaurants = r.findRestaurants(…);
…
}
This test creates a
MyExampleCallback
containing the expected values. The mock
JdoTemplate
verifies that
executeFind()
is called with a
MyExampleCallback
that is
equal to the expected one. In chapters 5 and 6 you will see more examples of
repositories that have been implemented with this approach.
We have now seen an overview of the key
ORM
concepts and the capabilities of
JDO
and Hibernate. The next step is testing an application's persistence layer.
4.5 Testing a persistent domain model
Every six months, Anne-Marie, who is my dental hygienist, gives me the same lec-
ture on the importance of flossing. And each time, I half-heartedly promise that I
will make more of an effort—but I never keep that promise. Some developers
Search WWH ::
Custom Search