今天我尝试将一个带有集成测试的项目从 maven 切换到 gradle。一切都很好,除了我对 testng 有一个严重的问题。
该项目使用 hibernate/JPA2 进行数据库访问,并且有几个测试依赖于 test/resources/META-INF/persistence.xml 中的持久性单元。当我使用 gradle 运行测试套件时,一切正常。但是当我从eclipse运行xml(或任何测试类)时,它似乎试图使用main/resources/META-INF/persistence.xml。
因为我的大部分工作都是使用 TDD 完成的,所以我真的需要从 eclipse 运行/调试测试。当我将持久性单元添加到生产 persistence.xml 时,它可以工作(它甚至从“测试”目录中获取一些其他资源)。这将是一种解决方法,但我真的不喜欢将测试资源添加到“main/resources”的想法
当我使用来自 maven 的旧 pom.xml 导入相同的项目时,它工作正常。
构建.gradle
apply plugin: 'java'
apply plugin: 'eclipse'
sourceCompatibility = 1.7
version = '0.1'
jar {
manifest {
attributes 'Implementation-Title': 'md', 'Implementation-Version': version
}
}
repositories {
mavenCentral()
}
dependencies {
compile 'org.slf4j:slf4j-api:1.7.5'
compile 'com.google.guava:guava:14.0.1'
compile 'org.hibernate:hibernate-entitymanager:4.2.2.Final'
compile 'org.hibernate.javax.persistence:hibernate-jpa-2.0-api:1.0.1.Final'
testCompile 'ch.qos.logback:logback-classic:1.0.13'
testCompile 'org.testng:testng:6.8.5'
testCompile 'org.dbunit:dbunit:2.4.9'
testCompile 'org.mockito:mockito-all:1.9.5'
testCompile 'org.easytesting:fest-assert-core:2.0M10'
testCompile 'org.hsqldb:hsqldb:2.2.9'
}
test {
useTestNG(){
suites 'src/test/resources/testng.xml'
}
}
更新:
生成的类路径文件如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src/main/java"/>
<classpathentry kind="src" path="src/main/resources"/>
<classpathentry kind="src" path="src/test/java"/>
<classpathentry kind="src" path="src/test/resources"/>
<classpathentry exported="true" kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry exported="true" kind="con" path="org.springsource.ide.eclipse.gradle.classpathcontainer"/>
<classpathentry kind="output" path="bin"/>
</classpath>
它似乎将所有内容合并到 bin 文件夹中,并且不像 maven 生成的 .classpath 文件那样区分 main 和 test 。