Monday, April 7, 2014

Unit Testing JPA is Painful!!!

So, I decided since I'm rebuilding my BirdDog app from scratch, I'd take the time at each tier to implement thorough unit testing.  Should be easy enough, right.  Not so much.  First, there isn't any automatic help in NetBeans (or anywhere else it seems) for creating the necessary persistence objects in order to test the entity classes.  It is easy to create the JUnit Test classes, though.

So, I went in search of some help.  So far, I've found a few promising web pages.  I had a few bumps, but one thing remains broken.  I can't get the EntityManager to create properly.  UGH!!!!

Here is my current error:
Could not initialize class org.looman.birddog.data.test.PersistenceHelper.

PersistenceHelper is a static class for setting up a test database in Derby and creating the EntityManager.  Here's the code that's failing:

  private static final EntityManager entityManager;
  static {
    Properties props = new Properties();
    props.put("javax.persistence.transactionType", "RESOURCE_LOCAL");
    props.put("javax.persistence.jtaDataSource","");
    props.put("javax.persistence.nonJtaDataSource", "");
    props.put("eclipselink.jdbc.driver", "org.apache.derby.jdbc.EmbeddedDriver");
    props.put("eclipselink.jdbc.url", "jdbc:derby:testDB;create=true");
    props.put("eclipselink.jdbc.user", "");
    props.put("eclipselink.jdbc.password","");
    props.put("eclipselink.ddl-generation", "drop-and-create-tables");
    props.put("eclipselink.logging.level", "INFO");
    entityManager = Persistence.createEntityManagerFactory("BirdDogDataPU").createEntityManager();
  }

[EDIT 11-Apr-2014: I found a great source for formatting the code to make it more readable as code.  This is an implementation of SyntaxHighlighter by Alex Gorbatchev. David Craft provided easy to follow instructions for using this on his blog, CraftyFella's Blog.]

These are the two pages I found most useful.
Unit Testing your Persistence Tier code - By Neil Buesing on Nov. 9 2010
DevBlog: JPA Unit test setup with NetBeans - Joerg Gross

I'm done for the night.  Hopefully, the light will shine on soon.

On a slightly different note:
If anyone is following this, I have a question for you.  Would it be beneficial for me to upload my code into SourceForge?  Personally, I don't need the version control, etc.  However, if people are interested in seeing the code or helping me learn, then I'll go through the trouble of creating a project.  You have to reply in the comments and let me know you'd like to see this.

Good night.

No comments:

Post a Comment