Q:

Integration Tests with EJB, JPA and DBUnit

I want to write integration tests for an ejb that does some basic CRUD operations.
here is how my test looks like now:
@Test
public void testAdd() {
    EJBContainer container = javax.ejb.embeddable.EJBContainer.createEJBContainer();
    MyEJBLocal myEjb= (MyEJBLocal)container.getContext().lookup("java:global/classes/MyEJB");

    // assuming I empty the database in the setup
    myEjb.add();
    int numOfEntities= myEjb.getAll().size();
    Assert.assertTrue("Unexpected number of entities", (numOfEntities==1));
    container.close();
}

obviously, this is not a good test but how to properly integrate DBUnit or any similar framework to control the state of the database before and after the add() method?  
Iam using EJB, Hibernate and embedded Glassfish

A:

You need to use Arquillian to properly test your EJBs. Arquillian allows you to run integration tests which complement regular mocked-services unit tests.
I would advice you having two separate profiles:

one for using an in-memory db (HSQLDB, H2) running fast and validating JPQL/Criteria queries

another one connecting to a DB server clone you are using in your production environment.
This second approach is useful for testing database specific features you might use (window functions, common table expressions, native queries) and also transaction logic (dead-locks, optimistic locking failures)

