【发布时间】:2014-11-27 16:03:38
【问题描述】:
我有一个 JPA/Hibernate 项目,它实现了许多存储库接口(我使用 DDD),我还有另一个项目使用 JUnit 4 测试第一个项目。我的 IDE 是 Eclipse Luna。后来我才知道,我可以用运行时和测试代码制作一个项目,但是现在,我将它们分开在这两个项目中。
到目前为止一切都很完美。
现在,我决定将我的测试项目转换为 Gradle 项目。转换后,我创建了这个build.gradle 文件
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'application'
tasks.withType(JavaCompile) {
options.encoding = "UTF-8"
}
repositories {
mavenLocal()
mavenCentral()
}
jar {
baseName = 'dominio.hibernate.tests'
version = '0.1.0'
}
dependencies {
compile 'junit:junit:4.11'
compile 'org.hamcrest:hamcrest-all:1.3'
runtime fileTree(dir: '../dominio/build/libs', include: '*.jar')
compile fileTree(dir: '../dominio/build/libs', include: '*.jar')
runtime fileTree(dir: '../dominio.hibernate/build/libs', include: '*.jar')
compile fileTree(dir: '../dominio.hibernate/build/libs', include: '*.jar')
compile 'postgresql:postgresql:9.0-801.jdbc4'
compile 'org.hibernate:hibernate-core:4.3.6.Final'
compile 'org.hibernate:hibernate-c3p0:4.1.0.Final'
compile "javax.enterprise:cdi-api:1.0-SP1"
compile "javax.servlet:servlet-api:2.5"
runtime "org.jboss.weld:weld-core:1.0.1-SP1"
compile project(':Dominio')
compile project(':DominioHibernate')
}
task wrapper(type: Wrapper) {
gradleVersion = '1.11'
}
编译工作完美,但是当我运行测试时,在 Eclipse 或 Gradle 中,我有以下异常
javax.persistence.PersistenceException: No Persistence provider for EntityManager named unidades
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:61)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:39)
at dominio.hibernate.tests.HibernateUnidadeOrganizacionalRepositoryTests.setUp(HibernateUnidadeOrganizacionalRepositoryTests.java:36)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
... the rest of the stack
我强调 Java 代码没有任何变化,在 Gradle 出现之前它运行完美。
因此,在将项目转换为 Gradle 后,在 JUnit 运行期间似乎无法再访问 persistence.xml,并且我无法获得我需要执行测试的 EntityManager 实例。
我浏览了大量关于 JUnit、资源文件和 Gradle 相关问题的帖子,但没有人能告诉我如何解决问题。
有什么想法吗?提前致谢!
【问题讨论】:
-
请编辑标题以明确问题是 Hibernate/EntityManager 所特有的。
标签: java eclipse hibernate junit gradle