Java Reference
In-Depth Information
F
testPendingOrderMapping() verifies the mapping for the line items.
This method verifies that all fields are mapped by calling assertAllFieldsMapped() .
As you can see, writing a comprehensive test for the mapping requires a lot of
work, but sometimes it is worthwhile. Let's now look at how to verify that the
schema matches the O/R mapping.
G
Verifying that the schema matches the mapping
Another part of testing the O/R mapping is verifying the existence of all of the
database tables and columns that it references. Of course, if the schema is auto-
matically generated from the O/R mapping then we don't have to do this. How-
ever, in many applications the schema is maintained separately and so can
potentially be inconsistent with the O/R mapping. ORMU nit makes it easy to verify
that the database schema matches the O/R mapping:
public class FoodToGoSchemaTests extends HibernateSchemaTests {
public void test() throws Exception {
assertDatabaseSchema();
}
}
This test calls assertDatabaseSchema() , which was described earlier, to verify that
there are no missing columns. It will catch common mistakes such as defining the
O/R mapping for a new field without adding the corresponding column to the
schema. Because it can check that the schema matches the O/R mapping for all
classes, we only need to write it once.
Now that we have written tests for the O/R mapping, let's look at how to write
tests that create, find, update, and delete persistent objects.
Writing persistence tests
We are almost done with the tests. The last set of tests we must write are those that
create, update, and delete PendingOrder s. These tests are necessary because some-
times incorrectly defined constraints can prevent objects from being persisted
and associations from being formed and destroyed. Consequently, it is useful to
write tests that take a persistent object through its lifecycle.
Because writing these kinds of tests can be time consuming, you might want to
start off with a really simple test such as the following, which creates and saves a
PendingOrder :
public class HibernatePendingOrderPersistenceTests extends
HibernatePersistenceTests {
 
 
 
Search WWH ::




Custom Search