11

I would like to add an additional "source set" to a Kotlin project that will contain integration tests. I have seen a few posts that talk about doing it for either a vanilla Java project or for Kotlin but using Groovy rather than the Kotlin Gradle DSL.

In summary, using the Kotlin Gradle DSL:

  • how to add an additional "source set" that can contain Kotlin code, Java code & resources for the purpose of separating integration tests from regular unit tests?
  • how to add an additional task and configuration to run the integration tests separately from unit tests?

I would expect the directory structure to look something like:

src
   main
      java
      kotlin
      resources
   test
      java
      kotlin
      resources
   integration
      java
      kotlin
      resources

Related:

Thanks

eskatos
  • 4,174
  • 2
  • 40
  • 42
cdc
  • 2,511
  • 2
  • 17
  • 15
  • 2
    I just created a question that's related to yours. I was also able to figure out how to get it to work. https://stackoverflow.com/questions/52904603/integration-tests-with-gradle-kotlin-dsl/52906232#52906232 – Johan Vergeer Oct 20 '18 at 13:35

2 Answers2

1

First, create source set and configuration:

sourceSets {
    create("intTest") {
        compileClasspath += sourceSets.main.get().output
        runtimeClasspath += sourceSets.main.get().output
    }
}

val intTestImplementation: Configuration by configurations.getting {
    extendsFrom(configurations.implementation.get())
}

val intTestRuntimeOnly: Configuration by configurations.getting {
    extendsFrom(configurations.runtimeOnly.get())
}

And then, create the task to run them:

val integrationTest = task<Test>("integrationTest") {
    description = "Runs integration tests"
    group = "verification"

    testClassesDirs = sourceSets["intTest"].output.classesDirs
    classpath = sourceSets["intTest"].runtimeClasspath
    shouldRunAfter("test")
}

Also, you can add dependencies to be used by the new source set. For instance:

intTestImplementation("org.junit.jupiter:junit-jupiter-api:$junitVersion")
intTestRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:$junitVersion")
Héctor
  • 24,444
  • 35
  • 132
  • 243
-1

You must add following configuration to your build.gradle file

configurations {
    integrationTestImplementation.extendsFrom implementation
    integrationTestRuntimeOnly.extendsFrom runtimeOnly
}


dependencies {
    intTestImplementation 'junit:junit:4.12'
    ...
}

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

task integrationTest(type: Test, dependsOn: []) {
    testClassesDirs = sourceSets.integrationTest.output.classesDirs
    classpath = sourceSets.integrationTest.runtimeClasspath
    useJUnitPlatform()
}

source: https://docs.gradle.org/current/userguide/java_testing.html#sec:configuring_java_integration_tests

TCH
  • 421
  • 1
  • 6
  • 25