POJO Fixture - the alrounder for testing complex object graphs or complex queries

The POJO fixture provides access to the entity manager, it creates the schema for you if you are using Ejb3Unit with the in memory database and provides helper methods to persist and generate random instances of your persistent objects.

POJO fixture is suitable to test complex object graphs of your domain objects or test complex database operations (by testing complex queries). The POJO gives you access to the EntityManager "this.getEntityManager()" and provides a convenient way to store, retrieve and compare (using "assertCollectionsEqual(..)") complex persistent objects.

Additionally POJO Fixture provides methods to generate random instances of your persistent objects. POJO Fixture randomly fills all primitive typed fields (int, short,..), their object representatives (Integer, Short,..) plus String and Date fields.

public class PojoFixtureUsageExampleTest extends PoJoFixture {

        private static final Class<?>[] USED_ENTITIES = { Order.class,
                        LineItem.class };

        public PojoFixtureUsageExampleTest() {

        public void testToWriteComplexObjectGraph() {
                List<Order> complexObjectGraph = {.. build your own graph here...}

                // persist the graph and load it again
                List<Order> persisted = persist(complexObjectGraph);
                List<Order> allFromDB = findAll(Order.class);

                // assert the persisted graph and the loaded are equal
                assertCollectionsEqual(persisted, allFromDB);

To generate random instances you can use the following method:

Order radomOrder = this.generateRandomInstance(Order.class);

To delete the inserted data you can use:

    protected void tearDown() throws Exception {

POJO fixture supports also the concept of Data sets to put the underlying database in an certain stage and deletes these data afer each test execution again.