title: Unit Testing database layer DAO with derby in memory
body: <p>I want to unit tests my DAO implementation with this:</p>
  
  <pre><code>public class BaseDAOImplTest {
  
      private EntityManagerFactory emFactory;
      private EntityManager em;
      private static Logger logger = Logger.getLogger(BaseDAOImplTest.class
              .getName());
  
      @Before
      public void setUp() throws SQLException, ClassNotFoundException {
  
          logger.info("Starting in-memory database for unit tests");
  
          try {
  
              //Creating testDB database
              Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
              DriverManager.getConnection("jdbc:derby:memory:TestingDB;create=true");
              logger.info("Building JPA EntityManager for unit tests");
  
              //Creating Entity Manager
              emFactory = Persistence.createEntityManagerFactory("TestingDB");
              em = emFactory.createEntityManager();
          } catch (ClassNotFoundException e) {
  
              e.printStackTrace();
          }
      }
  
      @Test
      public void testPersistExcursion() {
          BaseDAOImpl baseDao = new BaseDAOImpl(Excursion.class, em);
          Calendar c = Calendar.getInstance();
          c.set(2013, 8, 24);
          Excursion ex1 = createExcursion(c.getTime(), 10, "Simple excursion 1", "New York", 35);
          ex1 = baseDao.persist(ex1);
          assertEquals(baseDao.findById(ex1.getId()), ex1);
      }
  
      @Test
      public void testPersist() {
          try {
              Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
              String dbURL = "jdbc:derby:memory:TestingDB;create=true";
              Connection conn = getConnection(dbURL);
              EntityManagerFactory f = Persistence.createEntityManagerFactory("TestingDB");
              EntityManager em = f.createEntityManager();
              BaseDAOImpl b = new BaseDAOImpl(Excursion.class, em);
              Calendar c = Calendar.getInstance();
              c.set(2013, 8, 24);
              Excursion ex1 = createExcursion(c.getTime(), 10, "Simple excursion 1", "New York", 35);
              ex1 = b.persist(ex1);
              assertEquals(b.findById(ex1.getId()), ex1);
          } catch (ClassNotFoundException ex) {
              Logger.getLogger(BaseDAOImplTest.class.getName()).log(Level.SEVERE, null, ex);
          }
      }
  }
  </code></pre>
  
  <p><code>testPersist</code> is working and result is that this test passed. But in <code>testPersistExcursion</code> I get error with <code>NullPointerException</code> because on <code>setUp</code> I get:</p>
  
  <pre><code>java.lang.ClassNotFoundException: org.apache.derby.jdbc.EmbeddedDriver
  </code></pre>
  
  <p>I just wanted to add <code>EntityManager</code> as global and initialize in <code>setUp</code> so I don't need to write these lines again in every unit test (I saw it in some article and I think it's good aproach). But what is problem with this? Why is it working in unit test but in <code>setUp</code> I can't find that driver? Thanks</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>I found problem and that was in maven. I have wrong artifactId. But now I have problem with <code>"Unable to build EntityManagerFactory"</code> at this line: <code>emFactory = Persistence.createEntityManagerFactory("TestingDB");</code></p>
  
  <p>My persistence unit:</p>
  
  <pre><code>&lt;persistence-unit name="TestingDB" transaction-type="RESOURCE_LOCAL"&gt;
      &lt;class&gt;cz.infi.javatravelagency.entities.Reservation&lt;/class&gt;
      &lt;class&gt;cz.infi.javatravelagency.entities.Trip&lt;/class&gt;
      &lt;class&gt;cz.infi.javatravelagency.entities.Customer&lt;/class&gt;
      &lt;class&gt;cz.infi.javatravelagency.entities.Excursion&lt;/class&gt;
      &lt;exclude-unlisted-classes&gt;false&lt;/exclude-unlisted-classes&gt;
      &lt;properties&gt;
        &lt;property name="hibernate.connection.username" value="APP"/&gt;
        &lt;property name="hibernate.connection.driver" value="org.apache.derby.jdbc.EmbeddedDriver"/&gt;
        &lt;property name="hibernate.connection.password" value="APP"/&gt;
        &lt;property name="hibernate.connection.url" value="jdbc:derby:memory:TestingDB;create=true"/&gt;
        &lt;property name="hibernate.connection.autoReconnect" value="true" /&gt;  
        &lt;property name="hibernate.connection.autoReconnectForPools" value="true" /&gt;  
        &lt;property name="hibernate.dialect" value="org.hibernate.dialect.DerbyDialect" /&gt;  
      &lt;/properties&gt;
    &lt;/persistence-unit&gt;
  </code></pre>