Java Reference
In-Depth Information
6.3.2
Testing persistent Hibernate objects
This section explores the persistence tests you need to write for the PendingOrder
class. Even though ORMU nit makes it easier to write tests, it can still be time con-
suming to develop thorough persistence tests. Consequently, we describe how to
start off with a simple test and then add more elaborate ones.
Verifying the O/R mapping
We need to write tests that verify that the O/R mapping correctly maps the Pend-
ingOrder class to the PENDING_ORDER table. We must verify that each persistent
class is mapped to the correct table and that each field is mapped to the correct
database column, foreign key, or join table. A good way to do this is to write tests
for the ORM documents using the Hibernate version of ORMU nit, which verifies
the O/R mapping by using Hibernate metadata API s. This approach is much eas-
ier than using DbUnit to verify the contents of the database. The tests also run
much faster.
Here is a very simple O/R mapping test for the PendingOrder class. FoodToGo-
HibernateMappingTests extends the ORMU nit HibernateMappingTests class and
defines a testPendingOrderMapping() method, which make basic assertions about
the PendingOrder class's O/R mapping:
public class FoodToGoHibernateMappingTests extends
HibernateMappingTests {
public void testPendingOrderMapping() throws SQLException,
HibernateException {
assertClassMapping(PendingOrder.class, "PENDING_ORDER");
assertAllFieldsMapped();
}
This test verifies that the PendingOrder class is mapped to the PENDING_ORDER
table and that all of its fields are mapped to the database. It detects the common
problem of forgetting to define the mapping for a newly added field.
This simple test is a good start, but sometimes it is useful to write a test that ver-
ifies that each field is mapped correctly to the database. Here is a more elaborate
test that makes assertions about the O/R mapping for each field of the Pending-
Order class:
 
 
 
 
Search WWH ::




Custom Search