Java Reference
In-Depth Information
The query consists of a join between the restaurant and its time ranges. A restau-
rant is selected if its serviceArea field contains the zipCode and it has a TimeRange
that matches the specified time.
6.4.4
Writing tests for a query
At this point, you were probably hoping to be done, but alas there is one more set
of tests that we must write. The where clause of the query we just wrote contains
several relational operators. As you saw in chapter 4, it's a good idea to test it with
various combinations of data. Each of the tests for this query, some of which are
shown in listing 6.6, initializes the database with test data, invokes the query with a
particular set of arguments, and verifies that it returns the expected results. The
test class extends the ORMU nit class HibernatePersistenceTests and uses the
RestaurantMother helper class to construct a test restaurant in the database.
Listing 6.6
HibernateRestaurantRepositoryQueryTests
public class HibernateRestaurantRepositoryQueryTests extends
HibernatePersistenceTests {
private static final String GOOD_ZIP_CODE = "94619";
private static final String BAD_ZIP_CODE = "94618";
B Initializes database
protected void setUp() throws Exception {
super.setUp();
delete(Restaurant.class);
Restaurant r = RestaurantMother
.makeRestaurant(GOOD_ZIP_CODE);
save(r);
}
C Executes query
private void findAvailableRestaurants(
bbbbbbbbbbbbb b b
int dayOfWeek, int hour,
int minute, String zipCode, boolean expectRestaurants)
throws Exception {
String[] paramNames = { "zipCode", "dayOfWeek", "hour",
"minute" };
Object[] paramValues = new Object[] { zipCode,
new Integer(dayOfWeek), new Integer(hour),
new Integer(minute) };
List availableRestaurants = getHibernateTemplate()
.findByNamedQueryAndNamedParam(
"findAvailableRestaurants", paramNames,
paramValues);
if (expectRestaurants)
assertFalse(availableRestaurants.isEmpty());
 
 
 
Search WWH ::




Custom Search