Java Reference
In-Depth Information
This test verifies that the PendingOrder class uses application identity and that the
ID field is mapped to the PENDING_ORDER_ID column. It also verifies the restau-
rant field is mapped to a foreign key column called RESTAURANT_ID . Writing this
test can take a while, but it is a lot easier than verifying the contents of the database.
I often find it useful to first write some very simple tests that verify that the class
is mapped to the right table and that all of its fields are mapped to the database.
This initial step detects many common problems. I then expand the tests over
time to verify the correct mapping for each field.
Verifying that the schema matches the mapping
The second kind of test that you need to write is one that verifies that the database
schema matches the O/R mapping. ORMU nit makes it easy to verify that the data-
base schema matches the O/R mapping:
public class JDOFoodToGoSchemaValidationTests extends
JDOSchemaTests {
public void test() throws Exception {
assertDatabaseSchema();
}
}
This test calls assertDatabaseSchema() , which we 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 checks that the database schema matches the O/R map-
ping for all classes, we only need to write it once.
Now that we have written tests to verify that the O/R mapping and the database
schema, let's look at writing tests that persist JDO objects.
Verifying that objects can be created, queried, updated, and deleted
The third and final kind of tests is one that verifies that the PendingOrder can be
created, queried, updated, and deleted. A good way to begin is to write a test that
simply saves the PendingOrder in the database and then write more elaborate
tests later. Here is a test for PendingOrder that does just that:
public class JDOPendingOrderPersistenceTests extends
JDOPersistenceTests {
public void testPendingOrderSimple() throws Exception {
doWithTransaction(new TxnCallback() {
public void execute() throws Exception {
PendingOrder po = new PendingOrder();
 
 
 
Search WWH ::




Custom Search