JUnit, JPA, Hibernate and Postgres: how to test?

I got stuck for a while. I searched a lot and I cannot find the easiest way to check entity classes or JPA operations against postgres database. I have found how to use Spring, Mockito and other things, but I cannot find the easiest way to use pure Java.

I have the following JUnit test:

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."); } } 

and when I start, I get the following error:

 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.<init>(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.<init>(TestClass.java:45) at org.junit.runners.ParentRunner.<init>(ParentRunner.java:73) at org.junit.runners.BlockJUnit4ClassRunner.<init>(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) 

My persistence.xml file:

 <?xml version="1.0" encoding="UTF-8"?> <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"> <persistence-unit name="PU" transaction-type="JTA"> <provider>org.hibernate.ejb.HibernatePersistence</provider> <jta-data-source>jdbc/modelsystemdb</jta-data-source> <exclude-unlisted-classes>false</exclude-unlisted-classes> <properties/> </persistence-unit> </persistence> 

What to do to run a trial run?

+4
source share
5 answers

Quote Problem with distorted Java EE 6 APIs in Maven repository and solution from Adam Bien:

Instead of using

 <dependency> <groupId>javax</groupId> <artifactId>javaee-web-api</artifactId> <version>6.0</version> <scope>provided</scope> </dependency> 

You should use alternative (geronimo, jboss, etc.) dependencies:

 <dependency> <groupId>org.apache.geronimo.specs</groupId> <artifactId>geronimo-ejb_3.1_spec</artifactId> <version>1.0</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.apache.geronimo.specs</groupId> <artifactId>geronimo-jpa_2.0_spec</artifactId> <version>1.0</version> <scope>provided</scope> </dependency> 
+3
source

There was a similar problem. ie Although the Hibernate JPA JAR was available through transitive dependencies and a javaee 6.0 dependency was provided, the JUNIT Test was unsuccessful. I simply moved the javaee dependency at the end of the dependency definition in the pom file, so that the hibernate JPA api jar appeared in front of the Javaee bank while resolving the class path. It seemed like a trick, and I was able to run a test case. Hope this helps.

  <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>${hibernate.version}</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-entitymanager</artifactId> <version>${hibernate.version}</version> </dependency> 

javaee appears after hibernation

 <dependency> <groupId>javax</groupId> <artifactId>javaee-api</artifactId> <version>6.0</version> <scope>provided</scope> </dependency> 
+2
source

If you want to use JPA in your tests, then you will need to have a JPA provider in the classpath when it starts. You can compile with javaee-api , but at runtime you should have a real, live provider.

You mention using GlassFish; which uses EclipseLink as its provider, so it would be wise for you to do the same for your tests. There is information on using EclipseLink through Maven on their wiki.

+1
source

We have a very similar problem: Junit tests were successfully executed during the maven build, but trying to run them from Eclipse directly showed this terrible "Missing Code" error. The solution is not in pom.xml-s (they should be right, since your build is working, right?), But the configuration configuration for Junit is in eclipse. In our case, when the maven dependencies were moved on top of the project itself, the implementation persistence classes (eclipselink 2.0.4) were loaded. Therefore, in eclipse, try checking "Run Configurations ...", select the Junit configuration of interest and the "Classpath" tab, rearrange the libraries in the "User Entries" section: move the Maven dependencies on top.

Regards, Joe Public

0
source

In my case, in the Run Configurations / ClassPath section of the project, click "Edit" and check "Include only exported entries" that did the trick.

0
source

All Articles