【问题标题】:intellij idea 14 and gradle issues when running integTests运行 integTests 时的 intellij idea 14 和 gradle 问题
【发布时间】:2015-05-06 05:26:14
【问题描述】:

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

如果我将相同的资源移动到 src/test/resources(或 src/main/resources),测试运行良好。所以看起来 intellij 不只是在 src/integTest/resources 下查找。

感谢帮助!

【问题讨论】:

    标签: intellij-idea gradle


    【解决方案1】:

    看起来这是 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 上发布的重复答案,此答案最初被版主删除。我现在在那里删除了我的答案(并在此处添加了一个链接),因为我认为它更适合这个问题。

    【讨论】:

      【解决方案2】:

      我之前也遇到过这种情况,请将以下内容添加到您的 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 中得到解决,但我尚未验证。您将需要更新特定用例的路径。

      【讨论】:

      • 感谢您的建议。第一次尝试,它仍然没有工作。我会多玩一点,看看它是否是我的一些配置。再次感谢!!
      猜你喜欢
      • 1970-01-01
      • 2015-01-14
      • 2016-12-21
      • 2016-02-09
      • 1970-01-01
      • 2023-03-03
      • 1970-01-01
      • 1970-01-01
      • 2020-07-05
      相关资源
      最近更新 更多