Java Reference
In-Depth Information
4.5.2
Testing against the database
Tests that run against the database are an essential part of testing the persistent
domain model even though they take a relatively long time to execute. There are
two kinds of database-level tests. The first kind verifies that persistent objects can
be created, updated, and deleted. The second kind verifies the queries that are
used by the repositories. Let's look at each approach.
Testing the persistent objects
One goal of testing the persistent domain model is to verify that persistent objects
can be saved in the database. A simple approach is to write a test that creates a
graph of objects and saves it in the database. The test doesn't attempt to verify
that the database tables contain the correct values and instead fails only if an
exception is thrown by the ORM framework. This kind of test is a relatively easy
way to find basic ORM bugs, including missing mappings for a class and missing
database columns. It also verifies that the database constraints allow new objects
to be inserted into the database. However, even though this kind of test is a good
way to start, it does not detect other common ORM bugs, such as constraint viola-
tions that occur when objects are updated, added, or deleted.
We can catch those types of bugs by writing more elaborate tests that update
and delete persistent objects. As well as saving an object in the database, a test
loads the object, updates it, and saves it back. A test can also delete the object. For
example, a test for PendingOrder could consist of the following steps:
Create a PendingOrder and save it.
1
Load it, update the delivery information, and save it.
2
Load it, update the restaurant, and save it.
3
Load it, update the quantities, and save it.
4
Load it, update the quantities, and save it (again to test deleting line items).
5
Load it, update the payment information, and save it.
6
Delete the PendingOrder .
7
This testing approach verifies that the database can store all states of an object
and detects problems with database constraints when creating or destroying asso-
ciations between objects. Each step of the test consists of a database transaction
that uses a new persistence framework connection to access the database. Using a
new transaction and connection each time ensures that objects are really persisted
in the database and loaded again. It also makes sure that deferred constraints,
 
 
 
Search WWH ::




Custom Search