0

In my Java gradle project I use the JPA (with persistence.xml) to access a database and I want to run JUnit4 tests.

My build.gradle file contains these lines:

    ...
    providedCompile group: 'org.eclipse.persistence', name: 'javax.persistence', version: '2.1.0'
    testCompile group: 'org.hamcrest', name: 'hamcrest-all', version: '1.3'
    testCompile 'junit:junit:4.12'

    sourceSets{    
        main{
            java{
                srcDir 'src/java'
                srcDir 'test/java'
            }
        }
        test {
            java {
              srcDir 'unitTests/src'
            }
        } 
    }
    ...

the persistence.xml file is located under src/conf

The (Web-)application runs fine -> persistence provider & persistence.xml is found.

However when running JUnit tests an exception is thrown when the database is accessed:

javax.persistence.PersistenceException: No Persistence provider for EntityManager named xyz at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:85)

I tried several things like adding

 testCompile group: 'org.eclipse.persistence', name: 'eclipselink', version: '2.5.2'
 testCompile group: 'org.eclipse.persistence', name: 'javax.persistence', version: '2.1.0'
 testCompile group: 'org.eclipse.persistence', name: 'org.eclipse.persistence.jpa.jpql', version: '2.5.2'

to build.gradle

or copying persistence.xml to the unitTests directory

but I always receive the exception above :-(

Any more suggestions what might go wrong and what might help are appreciated!

HoWei
  • 21
  • 5
  • This is a dup of many many posts on here. You get that message IF EITHER the JPA provider selected is not in the CLASSPATH, OR the `persistence.xml` is not found under META-INF from the root of the CLASSPATH. Only you can check which one of those you have got wrong –  Jan 08 '19 at 07:46
  • Possible duplicate of [No Persistence provider for EntityManager named](https://stackoverflow.com/questions/1158159/no-persistence-provider-for-entitymanager-named) – piotr szybicki Jan 08 '19 at 08:00
  • paste your `persistence.xml` here. If you copy the file to the correct directory during testing then the missing tag there is the only option. – piotr szybicki Jan 08 '19 at 08:01
  • I am pretty sure that my persistence.xml is correct since the application itself runs fine! The JUnit tests start as well which means that the hamcrest and junit jar files are found. – HoWei Jan 09 '19 at 08:23
  • So the junit and hamcrest are in the correct classpath for the tests. But why not the JPA provider? I assume that there is something wrong with the build.gradle part for tests or the persistence.xml is in the wrong dir or I have to tell the test task where to find the persistence.xml – HoWei Jan 09 '19 at 08:41

0 Answers0