Java Reference
In-Depth Information
.with(eq("findAvailableRestaurants"),
eq(expectedNames),
eq(expectedValues))
.will(returnValue(expectedRestaurants));
E
Configures mock
HibernateTemplate
F
Calls
findAvailableRestaurants()
List foundRestaurants =
repository.findAvailableRestaurants(
deliveryAddress, deliveryTime);
G
Verifies
the return value
assertEquals(expectedRestaurants,
foundRestaurants);
}
private Date makeDeliveryTime(int dayOfWeek, int hour,
int minute) {
Calendar c = Calendar.getInstance();
c.set(Calendar.DAY_OF_WEEK, dayOfWeek);
c.set(Calendar.HOUR_OF_DAY, hour);
c.set(Calendar.MINUTE, minute);
return c.getTime();
}
}
Let's look at the details:
The
setUp()
method creates the mock
HibernateTemplate
.
The
setUp()
method creates the
HibernateRestaurantRepositoryImpl
, passing
the mock
HibernateTemplate
to its constructor.
The test creates some test data, including the parameters that are passed to
HibernateRestaurantRepository.findAvailableRestaurants()
and the parame-
ters that are expected to be passed to
HibernateTemplate.findByNamedQueryAnd-
NamedParam()
.
The test configures the mock
HibernateTemplate
to expect its
findByNamedQuery-
AndNamedParam()
method to be called with particular arguments.
The test calls
findAvailableRestaurants()
The test verifies that it returns list of restaurants that was returned by the mock
HibernateTemplate
.
As you can see, mock objects enable you to test the repositories without calling
Hibernate or the database. The tests are easy to write and execute very quickly. Of
course, the mock tests are only one part of the test suite; later on we will write the
tests for the query. But now let's write the code to get this test to compile and pass.
B
C
D
E
F
G
Search WWH ::
Custom Search