Skip to content

Custom test source set with Gradle Kotlin DSL and Intellij

My project is using Gradle with Kotlin DSL for building. I now want to add a custom sourceset used for testing. The relevant code looks as follows:

java {
    sourceSets.create("systemTest") {
        java.srcDir("src/system-test/java")
        resources.srcDir("src/system-test/resources")
    }
}

By using that, I get another module in Intellij, which is treated as source module. I also tried the following to mark it as test module:

idea.module {
 testSourceDirs.addAll(project.sourceSets.getByName("systemTest").java.srcDirs)
testSourceDirs.addAll(project.sourceSets.getByName("systemTest").resources.srcDirs)
}

However, if I then do a ./gradlew clean idea and open the project using the generated files (normally I import my projects from Gradle), the system-test folder isn’t treated as source module at all.

Anyone having experience with declaring a custom sourceset with Kotlin DSL AND marking it as test module?

EDIT: I now created a sample Gradle project using Groovy and there it worked without problems with using the following code (which I guess is just the groovy version of my statements above):

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

idea {
    module {
        testSourceDirs += project.sourceSets.systemTest.java.srcDirs
        testSourceDirs += project.sourceSets.systemTest.resources.srcDirs
    }
}

So either I am getting the transformation to Kotlin DSL wrong or it is a bug on the Intellij side.

Answer

Seems like I got the Kotlin transformation wrong, it should be

idea.module {
    val testSources = testSourceDirs

    testSources.addAll(project.sourceSets.getByName("systemTest").java.srcDirs)
    testSources.addAll(project.sourceSets.getByName("systemTest").resources.srcDirs)

    testSourceDirs = testSources
}

Using this snippet, the testSourceDirs set is changed and the relevant sourceset marked as test module.