Seiten

JPA Unit test setup with NetBeans


For the development of server projects I will use NetBeans 7.0.1 as IDE. It seems to be very easy to set up a JEE web project with this IDE. This will be the first time for me developing with NetBeans.

So I have started with a simple server project. Just one simple persistent entitiy: Person.

The Person class looks like this, pretty simple:
@Entity  
public class Person implements Serializable {  
      
    @Id  
    String uniqueId;  
      
    private String firstname;  
    ...  

Although I know that the persistence of JPA-Entities needs not to be unit tested, there are scenarios where it is useful to write unit tests to test the persistence tier, e.g. in case of complex queries (SQL, JPA query, etc). So I try to set up a test environment with an in memory Apache Derby database to execute unit tests in the IDE.

It was not so easy in the unit test scenario to tell the JPA-Provider (EclipseLink) which classes are the persistent ones. By default the JPA provider scans all jar files that contain a META-INF/persistence.xml file and all classes with an @Entity annotation are considered. I will rely on this mechanism in my project. Unfortunately during test execution there is no jar file to scan and so none annotated classes are found. And there seems to be no other way to activate a different xml-file based JPA configuration during test execution. Although there is a way to overload EntityManager properties when creating the EntityManager instance in the test set up - but I found no way to add a list of persistent classes:

@Before  
public void setUp() {  
    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");  
     
    EntityManagerFactory emFactory = 
        Persistence.createEntityManagerFactory("eercp.server.PU", props);  
    em = emFactory.createEntityManager();  
}  

Finally I found a way to tell EclipseLink which classes are persistent by overloading an ant target in the file build.xml of the NetBeans project. NetBeans executes this ant build script before test execution what gives us a chance to join in the execution process. So I placed an orm.xml file in the project and copy it together with the persistence.xml:

    <target if="has.persistence.xml" name="-copy-persistence-xml">  
      <mkdir dir="${build.web.dir}/WEB-INF/classes/META-INF"/>  
      <copy todir="${build.web.dir}/WEB-INF/classes/META-INF">  
        <fileset dir="${persistence.xml.dir}" >  
           <include name="persistence.xml"/>  
           <include name="orm.xml"/>  
        </fileset>  
      </copy>  
    </target> 

This does work - but it is not the way I would prefer. But it seems the only way to execute a JPA unit test with a different configuration in the NetBeans IDE.

The orm.xml-file just contains the list of persistent classes, the JPA provider merges this information with the annotations defined at the persistent classes:

  <entity-mappings version="1.0"  
      xmlns="http://java.sun.com/xml/ns/persistence/orm"  
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
      xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm orm_1_0.xsd">    

    <entity class="de.jgros.eercp.server.domain.User"/>  

  </entity-mappings>