25

I have an integration test source folder set up in gradle like so:

subprojects {
    apply plugin: 'java'
    apply plugin: 'idea'

    sourceCompatibility = 1.8

    configurations {
        integrationTestCompile.extendsFrom testCompile
        integrationTestCompileOnly.extendsFrom integrationTestCompile
        integrationTestCompileOnly.extendsFrom testCompileOnly
        integrationTestRuntime.extendsFrom testRuntime
    }

    sourceSets {
        integrationTest {
            java {
                compileClasspath += main.output + test.output
                runtimeClasspath += main.output + test.output
                srcDir file('src/integrationTest/java')
            }
            resources.srcDir file('src/integrationTest/resources')
        }
    }

    task integrationTest(type:Test) {
        testClassesDir = sourceSets.integrationTest.output.classesDir
        classpath = sourceSets.integrationTest.runtimeClasspath
        outputs.upToDateWhen { false }
    }
}

For executing the tests, this works perfectly well, but it causes problems with IntelliJ's inspections, which may change behavior for test code. IntelliJ does not recognize the source folder as test source.

I tried adding them as such (inside subprojects):

idea {
    module {
        testSourceDirs += file('src/integrationTest/java')
    }
}

but that did not help at all. I also tried manually marking them as test source (context menu -> mark directory as -> test sources root), but IntelliJ quickly overrides that back to normal source root.

How do I configure this correctly in Gradle?

I'm using IntelliJ 2016.1.3 and Gradle 2.14.1 on Ubuntu 16.04

Jorn
  • 20,612
  • 18
  • 79
  • 126
  • 3
    How did you regenerate your IDEA files after making changes? This should work (and I've tested it locally) - try running `$ ./gradlew cleanIdea idea` – tddmonkey Feb 06 '17 at 09:50
  • I'd rather have a solution where Gradle does not generate my Idea config, since there are also non-Gradle projects in the same repo. – Jorn Feb 06 '17 at 10:01
  • @tddmonkey Your solution does generate the correct `.iml` files, but IntelliJ isn't using them. Do you know how I get it to do that? The Java project is a subfolder of another, and that one is the main project with a `.idea` folder for config. – Jorn Feb 07 '17 at 15:16
  • How are you opening it? I generally do `$ ./gradlew cleanIdea idea; open .ipr` – tddmonkey Feb 07 '17 at 15:28

3 Answers3

15

You would need to make sure test source is the only source for this package then

idea {
    module {
        sourceDirs -= file('src/integrationTest/java')
        testSourceDirs += file('src/integrationTest/java')
    }
}

and then you would need to gradle cleanIdea idea to recreate the IntelliJ files.

be sure that you're not using IDE gradle integration when using idea plugin from gradle, custom changes to iml files will most likely clash with the IDE if the integration is on

sourceSets {
    integrationTest {
        java {
            compileClasspath += main.output + test.output
            runtimeClasspath += main.output + test.output
            srcDir "$projectDir/src/integrationTest/java"
        }
        resources.srcDir "$projectDir/src/integrationTest/resources"
    }
}

EDIT: Gradle 4.7 Idea plugin correctly marks sources now.

LazerBanana
  • 6,865
  • 3
  • 28
  • 47
  • 6
    Nope, this doesn't change anything. – Jorn Aug 21 '17 at 11:44
  • @Jorn try changing it like I suggested in the edit, not sure about your `file()` but I tend to specify the whole path to the source. As well do you use Idea plugin in Gradle or you use the Gradle integration in IntelliJ IDE? – LazerBanana Aug 21 '17 at 11:49
  • 1
    @bharal, it does work for others and me in that time so please elaborate, using a gradle plugin with IntelliJ gradle plugin at the same time creates a mix and confusion in few scenarios. Saying `it doesn't work` doesn't seems like a programmer approach. – LazerBanana Apr 10 '19 at 10:24
  • @LazerBanana idk what you want me to say - this does not work, intellij marks the folder as src, not test src. i'm using the gradle idea plugin - `apply plugin: 'idea'` and intellij has the gradle plugin. – bharal Apr 10 '19 at 13:56
  • @bharal so this will mean your IntelliJ plugin will probably override the custom iml changed made by the idea gradle plugin. If you use both that's why it will not work. But im ok with your -1 ;) i will add that explanation for others to see. Thanks – LazerBanana Apr 10 '19 at 14:43
  • It works! Everyone couple of weeks I revisit this answer, longing for the day the solution works. Today is that day. I noticed the `-=` for `sourceDirs`. So you're first *removing* "integrationTest" from "sourceDirs" and then adding it to "testSourceDirs". I never really noticed the `-=` before. I guess this is one of those cases where copy/paste is actually the right way to do it. – Paul Schwarz Feb 06 '21 at 13:28
  • It doesn't work if you have a `annotationProcessor` dependency. – cdalxndr Jun 16 '21 at 19:12
7

From JetBrains issue:

https://youtrack.jetbrains.com/issue/IDEA-151925#comment=27-2355076

apply plugin: 'java'
sourceSets {
  integrationTest
}
apply plugin: 'idea'
idea {
  module {
    testSourceDirs += project.sourceSets.integrationTest.java.srcDirs
    testSourceDirs += project.sourceSets.integrationTest.resources.srcDirs
  }
}
radistao
  • 14,889
  • 11
  • 66
  • 92
4

I know this is old but facing the same problem I found buried somewhere in this jetbrains ticket something hinting that your test sourceSet should be declared in gradle like so :

sourceSets {
    integrationTest {
        test{ //<- note this, this is what flags it right for intelliJ to process it correctly
            java {
                compileClasspath += main.output + test.output
                runtimeClasspath += main.output + test.output
                srcDir file('src/integrationTest/java')
                // note that you'll need to specify the outputDir for it to work
                // I don't fully get the reasons for this (but I rarely do when it comes to gradle anyway)
                outputDir = file("build/classes/java/integrationTest")
            }
            resources {
                srcDir file('src/integrationTest/resources')
            }
        }
    }
}

Doing so made intelliJ instantly recognize my integration-test folder like the test-source it is ...

[Small rant] I could not find trace of the appropriate corresponding gradle doc and as often I find info on how it works from retro-engineering or scarce hints in this case trial/error+ IntelliJ youtrack ticket (which says a lot on how poorly gradle is documented in my view ...)

Note that I am on

  • Intellij : IntelliJ IDEA 2021.3.2 (Ultimate Edition) Build #IU-213.6777.52
  • Gradle : 6.7.1
Ar3s
  • 2,237
  • 2
  • 25
  • 47