Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

OpenJPA Unit Tests are JUnit Tests. Base JUnit TestCase (org.junit.TestCase) has been extended to faciliate facilitate common initialization steps or configuration settings required for unit testing OpenJPA. As a test develpoerdeveloper, you should inherit your test class from one of the extended TestCases. This also helps the test you write to be integrated with OpenJPA test corpus of approximately 2000 test cases in 400 classes.
The inheritence inheritance hierarchy is:

No Formatcode
orgjunit.junitframework.TestCase
    org.apache.openjpa.persistence.test.PersistenceTestCase
        org.apache.openjpa.persistence.test.SingleEMFTestCase
            org.apache.openjpa.persistence.test.SQLListenerTestCase

...

The following example shows how to declare setUp() method in your test class

No Formatcode
public void setUp() throws Exception {
    setUp(CLEAR_TABLES,                      // clears rows from PERSON and ADDRESS table
          Person.class, Address.class,       // registers Person and Address as persistence-capable entity
          "openjpa.Multithreaded", "true");  // sets configuration property as name-value pair
}

...

The name of the persistence unit is, by default, "test". But your test should overwrite the name with a more specific unit name. For example,

No Formatcode
protected String getPersistenceUnitName() {
    return "test-eager-fetch";
}

...

JUnit TestCases invoke tearDown() method on termination. SingleEMFTestCase ensures that tearDown() method deletes all rows for the classes involved in your test. While clear-your-own-mess helps in most situations, you may want the database records to remain for analysis especially when tests are failing. In that case you may consider suppressing the superclass behavior of tearDown() by simply nullifying the method as

No Formatcode
public void tearDown() throws Exception {
     // avoids super class to delete all records
}

...

  • Domain classes should avoid common names such as Person or Address.
  • Prefer annotation over XML Descriptors for O-R Mapping because that helps to colocate relevant information. Unless, of course, the test is specifc specific about variations in behavior across annotation and XML Descriptors.