home.


Tagged: junit


JPA: Testing

You can test your JPA code by setting aside another folder structure, test/ for example, with a META-INF/ directory with a new persistence.xml file there pointing to a different database.

Here’s new new persistence.xml:

 <?xml version="1.0" encoding="UTF-8" ?>
 <persistence xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
     version="2.0"
     xmlns="http://java.sun.com/xml/ns/persistence">
   <persistence-unit name="example" transaction-type="RESOURCE_LOCAL">
     <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
     <class>org.denevell.tomcat.entities.write.AnotherThing</class>
     <properties>
       <property name="javax.persistence.jdbc.driver" value="org.sqlite.JDBC" />
       <property name="javax.persistence.jdbc.url" value="jdbc:sqlite:/home/YOURUSERNAME/test.db" />
       <property name="eclipselink.logging.level" value="ALL" />
       <property name="eclipselink.ddl-generation" value="drop-and-create-tables" />
     </properties>
   </persistence-unit>
</persistence>

The only different to the previous ones is that we’re putting our database in a new location - one where the runner of the junit test has write access. And we’re doing drop-and-create-tables in the table creation.

The next thing you need is the junit4 file that calls the persistence provider to test adding of the entity:

public class JPAStarterTest {
  @Test
  public void addAnotherThing() {
    // Arrange
    AnotherThing at = new AnotherThing();
    at.setText("hii");          
    // Act
    tx.begin();
    em.persist(at);
    tx.commit();            
    // Assert
    assertNotNull("Id should not be null", at.getId());
    List<AnotherThing> list = em.createNamedQuery("listAll", AnotherThing.class).getResultList();
    assertEquals("Table has one entity", 1, list.size()); 
    assertEquals("Table has correcttext", "hii", list.get(0).getText());
  }

  @BeforeClass
  public static void beforeClass() {
    emf = Persistence.createEntityManagerFactory("exampletest");
    em = emf.createEntityManager();
  }

  @AfterClass
  public static void afterClass() {
    em.close();
    emf.close();
  }

  @Before
  public void before() {
    tx = em.getTransaction();
  }
  private static EntityManagerFactory emf;
  private static EntityManager em;
  private EntityTransaction tx; 
}

Now when you run this, ensure your new persistence.xml is in the META-INF/ directory of the classes.

jpa java-testing jpa-testing junit

Ant junit

The junit tag take in a formatter tag to define the output, and classpath so it can find the classes, and a fileset within a batchtest tag to say where the test classes are (** means recursive).

<target name="junit" depends="compile">
    <junit printsummary="on" fork="true" haltonfailure="yes">
       <formatter type="plain" />
       <classpath refid="junit.class.path" />
       <batchtest todir="${test.report.dir}">
         <fileset dir="${src.dir}">
          <include name="**/*Test.java" />
         </fileset>
       </batchtest>
    </junit>
</target>   

The printsummary, fork and haltonfailure options does as you would expect. Batchtest uses the report dir to place the reports. You can go there to see the test output.

ant ant-junit

Page 1 of 1