6

今天我尝试将一个带有集成测试的项目从 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 。

4

2 回答 2

13

问题是 gradle 的 eclipse 插件默认合并了 test 和 main 文件夹。因此 main 中的 persistence.xml 确实覆盖了测试版本。

添加以下代码将通过更改生成的类路径条目的输出目录并删除具有默认输出的条目来解决问题。

import org.gradle.plugins.ide.eclipse.model.SourceFolder 
eclipse.classpath.file {
    beforeMerged { classpath -> 
        classpath.entries.clear()
    }
    whenMerged {  cp -> 
        cp.entries.findAll { it instanceof SourceFolder && it.path.startsWith("src/main/") }*.output = "bin/main" 
        cp.entries.findAll { it instanceof SourceFolder && it.path.startsWith("src/test/") }*.output = "bin/test" 
        cp.entries.removeAll { it.kind == "output" }
    }
}

更新:更改后的相关类路径条目。

<classpathentry output="bin/main" kind="src" path="src/main/java"/>
<classpathentry output="bin/main" kind="src" path="src/main/resources"/>
<classpathentry output="bin/test" kind="src" path="src/test/java"/>
<classpathentry output="bin/test" kind="src" path="src/test/resources"/>
于 2013-07-04T07:38:58.660 回答
1

将默认输出文件夹设置为“build”并将测试输出文件夹设置为“build-test”(使用 Gradle 2.7 测试):

eclipse.classpath {
    defaultOutputDir = file('build')
    file.withXml { n ->
        n.asNode().classpathentry.findAll { it.@path.startsWith('src/test') }
                .each { it.@output = 'build-test' }
    }
}

生成的 .classpath 文件条目:

<classpathentry kind="output" path="build"/>
<classpathentry kind="src" path="src/main/java"/>
<classpathentry kind="src" path="src/main/resources"/>
<classpathentry kind="src" path="src/test/java" output="build-test"/>
<classpathentry kind="src" path="src/test/resources" output="build-test"/>
于 2015-09-30T00:49:21.083 回答