intellij idea 14 和 gradle 在 运行 integTests 时出现问题

intellij idea 14 and gradle issues when running integTests

我有一个 gradle 项目导入到 idea 14.0.3 中。集成测试 运行 从命令行正常。他们也 运行ning 在上下文菜单中的想法 13 中没有任何问题(运行ning 单个测试)。但是,在 14 中,当我在 IDE 中使用上下文菜单时,出于某种原因,依赖于 src/integTest/resources 中的 class 路径资源的测试由于未找到资源而失败。知道如何在 Intellij 14 的 class 路径搜索中添加此文件夹吗?有没有人见过这个问题?

如果我将相同的资源移动到 src/test/resources(或 src/main/resources),测试 运行 没问题。所以看起来 intellij 并不是简单地在 src/integTest/resources.

下寻找

感谢帮助!!

我之前也有 运行,请将以下内容添加到您的 build.gradle 文件中:

// work-around to fix IDE-run test failures (may be fixed in future Gradle versions)
    task copyMainResourcesToTest(type: Copy) {
    from "${projectDir}/src/main/resources"
    into "${buildDir}/classes/test"
}
processTestResources.dependsOn copyMainResourcesToTest

task copyTestResourcesToTest(type: Copy) {
    from "${projectDir}/src/test/resources"
    into "${buildDir}/classes/test"
}
processTestResources.dependsOn copyTestResourcesToTest

我认为这可能会在 Gradle 的最新版本中得到解决,但我尚未验证。您将需要更新特定用例的路径。

看起来这是 IntelliJ 14 中的错误 (IDEA-128966)。 推荐的解决方法是这样的:

sourceSets {
    integrationTest {
        java.srcDir file('src/integTest/java')
        resources.srcDir file('src/integTest/resources')
    }
    if(System.properties.'idea.active') {
        main {
            resources.srcDir file('src/integTest/resources')
        }
    }
}

对于我们的项目,我将其更改为:

if(System.properties.'idea.active') {
    test { //Add to test rather than main

这仍然有效,我认为它更好地传达了意图。


此解决方法假定您已在 build.gradle:

中像这样配置集成测试
apply plugin: 'idea'

sourceSets { 
    integrationTest { 
        java.srcDir file('src/integTest/java') 
        resources.srcDir file('src/inteTest/resources') 
    } 
}

configurations { 
    integrationTestCompile.extendsFrom testCompile 
    integrationTestRuntime.extendsFrom testRuntime 
}

task integrationTest(type: Test, dependsOn: jar) { 
    testClassesDir = sourceSets.integrationTest.output.classesDir 
    classpath = sourceSets.integrationTest.runtimeClasspath 
}

build.dependsOn(integrationTest)

注意:由于我在 another question 上发布的重复答案,此答案最初被版主删除。我现在在那里删除了我的答案(并在此处添加了 link),因为我认为它更适合这个问题。