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!