title: JUnit, JPA, Hibernate and Postgres: How to test?
body: <p>I've been stuck for a while now. I have searched a lot and I can't find the easiest way to test entity classes or JPA operations against a postgres database. I've found how to using Spring, Mockito and other things, but I can't find the simplest way using pure Java.</p>
  
  <p>I have the following JUnit test: </p>
  
  <pre><code>public class ModelConverterTest {
  
      private static EntityManagerFactory emf;
      private static EntityManager em;
  
  
      public ModelConverterTest() {
      }
  
      @BeforeClass
      public static void setUpClass() throws Exception {
          emf = Persistence.createEntityManagerFactory("PU");
          em = emf.createEntityManager(); // Retrieve an application managed entity manager
      }
  
      @AfterClass
      public static void tearDownClass() throws Exception {
          em.close();
          emf.close(); //close at application end
      }
  
      @Before
      public void setUp() {
           ...    
      }
  
      @After
      public void tearDown() {
      }
  
      /**
       * Test of SIMModelToModel method, of class ModelConverter.
       */
      @Test
      public void testSIMModelToModel() {
          System.out.println("SIMModelToModel");
          SIMModel simModel = new PESMModel();
          simModel.addState(testState);
          Model expResult = null;
          Model result = ModelConverter.SIMModelToModel(em, simModel);
          assertTrue(expResult!=null);
          // TODO review the generated test code and remove the default call to fail.
          //fail("The test case is a prototype.");
      }
  }
  </code></pre>
  
  <p>and when running it, I get the following error:</p>
  
  <pre><code>java.lang.ClassFormatError: Absent Code attribute in method that is not native or abstract in class file javax/persistence/PersistenceContextType
      at java.lang.ClassLoader.defineClass1(Native Method)
      at java.lang.ClassLoader.defineClass(ClassLoader.java:787)
      at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
      at java.net.URLClassLoader.defineClass(URLClassLoader.java:447)
      at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
      at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
      at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
      at java.security.AccessController.doPrivileged(Native Method)
      at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
      at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
      at java.lang.Class.getDeclaredMethods0(Native Method)
      at java.lang.Class.privateGetDeclaredMethods(Class.java:2442)
      at java.lang.Class.getDeclaredMethods(Class.java:1808)
      at sun.reflect.annotation.AnnotationType$1.run(AnnotationType.java:104)
      at sun.reflect.annotation.AnnotationType$1.run(AnnotationType.java:101)
      at java.security.AccessController.doPrivileged(Native Method)
      at sun.reflect.annotation.AnnotationType.&lt;init&gt;(AnnotationType.java:100)
      at sun.reflect.annotation.AnnotationType.getInstance(AnnotationType.java:84)
      at sun.reflect.annotation.AnnotationParser.parseAnnotation(AnnotationParser.java:221)
      at sun.reflect.annotation.AnnotationParser.parseAnnotations2(AnnotationParser.java:88)
      at sun.reflect.annotation.AnnotationParser.parseAnnotations(AnnotationParser.java:70)
      at java.lang.reflect.Field.declaredAnnotations(Field.java:1033)
      at java.lang.reflect.Field.getDeclaredAnnotations(Field.java:1026)
      at java.lang.reflect.AccessibleObject.getAnnotations(AccessibleObject.java:196)
      at org.junit.runners.model.FrameworkField.getAnnotations(FrameworkField.java:26)
      at org.junit.runners.model.TestClass.addToAnnotationLists(TestClass.java:52)
      at org.junit.runners.model.TestClass.&lt;init&gt;(TestClass.java:45)
      at org.junit.runners.ParentRunner.&lt;init&gt;(ParentRunner.java:73)
      at org.junit.runners.BlockJUnit4ClassRunner.&lt;init&gt;(BlockJUnit4ClassRunner.java:55)
      at org.junit.internal.builders.JUnit4Builder.runnerForClass(JUnit4Builder.java:13)
      at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57)
      at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:29)
      at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57)
      at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:24)
      at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:51)
      at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:123)
      at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:104)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:601)
      at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:164)
      at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:110)
      at org.apache.maven.surefire.booter.SurefireStarter.invokeProvider(SurefireStarter.java:175)
      at org.apache.maven.surefire.booter.SurefireStarter.runSuitesInProcessWhenForked(SurefireStarter.java:107)
      at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:68)
  </code></pre>
  
  <p>My persistence.xml file is:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;persistence version="2.0" xmlns="http://java.sun.com/xml/ns/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"&gt;
    &lt;persistence-unit name="PU" transaction-type="JTA"&gt;
      &lt;provider&gt;org.hibernate.ejb.HibernatePersistence&lt;/provider&gt;
      &lt;jta-data-source&gt;jdbc/modelsystemdb&lt;/jta-data-source&gt;
      &lt;exclude-unlisted-classes&gt;false&lt;/exclude-unlisted-classes&gt;
      &lt;properties/&gt;
    &lt;/persistence-unit&gt;
  &lt;/persistence&gt;
  </code></pre>
  
  <p>What should I do to make the test run?</p>