user8681
user8681

Reputation:

Mark Gradle source folder as test source in IntelliJ

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

Upvotes: 26

Views: 19582

Answers (5)

Radosław Panuszewski
Radosław Panuszewski

Reputation: 536

Update 2024-10-20

Define your integration test source set via the official JVM Test Suite Plugin and IntelliJ will automatically mark it as a test source set :)

If you don't want to do it yourself, you can use the coditory/gradle-integration-test-plugin. Since version 2+, it uses the JVM Test Suite Plugin under the hood.

Original answer

With Gradle 7.4+ and Kotlin DSL you can do it like that:

plugins {
    `idea`
}

idea {
    module {
        // For sources 
        testSources.from(sourceSets.integration.get().allSource.srcDirs)
 
        // For resource files   
        testResources.from(sourceSets.integration.get().resources.srcDirs)
    }
}

The above example assumes that you use the coditory/gradle-integration-test-plugin (which registers the integration source set):

plugins {
    id("com.coditory.integration-test") version "1.4.5"
}

But should also work if you register your integration test source set in any other way. You may only need to change sourceSets.integration to sourceSets.yourNameOfTheSourceSet.

Upvotes: 3

Luan Kevin Ferreira
Luan Kevin Ferreira

Reputation: 1380

I had to add the following configurations to properly work (Gradle 8.5, IntelliJ 2023.3.2):

allprojects {
    // other plugins you might have
    apply plugin: "idea"
    // other configurations you might have
    sourceSets {
        intTest {
            compileClasspath += sourceSets.main.output
            runtimeClasspath += sourceSets.main.output
        }
    }
    
    idea {
        module {
            testSources.from(project.sourceSets.intTest.java.srcDirs)
            testResources.from(project.sourceSets.intTest.resources.srcDirs)
        }
    }
    /// other configurations you might have
}

Upvotes: 0

Ar3s
Ar3s

Reputation: 2307

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

Upvotes: 7

LazerBanana
LazerBanana

Reputation: 7211

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.

Upvotes: 15

radistao
radistao

Reputation: 15504

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
  }
}

Upvotes: 7

Related Questions