Java Reference
In-Depth Information
B
The constructor takes the class, the query name, and the query parameters as
parameters and stores them in fields.
The
equals()
method returns
true
if the type, the query name, and the query
parameters of the two objects are the same.
The
doInJdo()
method, which is invoked by the
JdoTemplate
with a
Persis-
tenceManager
, creates the named query and executes it.
Although this seems like a lot of code to do something so simple, it's important to
remember that this class is reusable and it makes the repository easier to test. Let's
see how.
C
D
Writing the mock object test
Once we have defined the
ExecuteNamedQueryWithMapCallback
class, we can
then write a mock object test for
findRestaurants()
. This test configures a mock
JdoTemplate
to expect its
execute()
method to be called with an instance of the
class that contains the expected query name and query parameters. Listing 5.5
shows the mock object test for
findRestaurants()
that does this.
Listing 5.5
JDORestaurantRepositoryImplTests
public class JDORestaurantRepositoryImplTests extends
MockObjectTestCase {
private Mock mockJdoTemplate;
private JDORestaurantRepositoryImpl repository;
private static final int EXPECTED_MINUTE = 6;
private static final int EXPECTED_HOUR = 5;
private static final int EXPECTED_DAY_OF_WEEK = 3;
Address deliveryAddress;
Date deliveryTime;
B
Creates mock objects
and test data
public void setUp() {
mockJdoTemplate =
new Mock(JdoTemplate.class);
repository = new JDORestaurantRepositoryImpl(
(JdoTemplate) mockJdoTemplate.proxy(), null);
restaurant = new Restaurant();
deliveryTime = makeDeliveryTime(EXPECTED_DAY_OF_WEEK,
EXPECTED_HOUR, EXPECTED_MINUTE);
![](POJOs-in-Action-Developing-Enterprise-Applications-with-Lightweight-Frameworks_images/img-gen761.png)
![](POJOs-in-Action-Developing-Enterprise-Applications-with-Lightweight-Frameworks_images/img-gen762.png)
![](POJOs-in-Action-Developing-Enterprise-Applications-with-Lightweight-Frameworks_images/img-gen763.png)
![](POJOs-in-Action-Developing-Enterprise-Applications-with-Lightweight-Frameworks_images/img-gen764.png)
![](POJOs-in-Action-Developing-Enterprise-Applications-with-Lightweight-Frameworks_images/img-gen765.png)
Search WWH ::
![Google](http://www.google.com/images/poweredby_transparent/poweredby_FFFFFF.gif)
Custom Search