Hi all, I’m running into some challenges while try...
# gradle
j
Hi all, I’m running into some challenges while trying to create integration tests using Gradle Kotlin DSL. I already just posted a question on StackOverflow. But I hope someone here can help me out with this one. https://stackoverflow.com/questions/52904603/integration-tests-with-gradle-kotlin-dsl
n
as for sourcesets:
Copy code
kotlin {
     sourceSets.maybeCreate("main").kotlin.apply {
                    srcDir(rootDir.resolve("something"))
                    srcDir(config.getGeneratedSrc)
                }
}
this is how i do it
i am pretty sure for something to be recognized as test source you will have to add them to
"test"
and that guide sets up
Copy code
compileClasspath += main.output + test.output
runtimeClasspath += main.output + test.output
for java, i think will want to do something similar
j
@Nikky I’m confused here. I have a Kotlin project, with no Java sources, but I should still use java config?
n
no, but you still need to depend on the output of test if you put your code into a differently named sourceSet as i said.. something similar
j
@Nikky Based on your first snipped, I created this
Copy code
kotlin {
    sourceSets {
        maybeCreate("integrationTest").kotlin.apply {
            srcDir("src/testIntegration/kotlin")
        }
        maybeCreate("integrationTest").resources.apply {
            srcDir("src/testIntegration/resources")
        }
}
Where would you put
Copy code
compileClasspath += main.output + test.output
runtimeClasspath += main.output + test.output
e
There’s some existing documentation around this. Adding an integration test source set at https://docs.gradle.org/release-nightly/userguide/organizing_gradle_projects.html#sec:separate_test_type_source_files Configuring a source set convention at https://docs.gradle.org/release-nightly/userguide/groovy_plugin.html#sec:changing_groovy_project_layout It’s respectively for java and groovy, but the same pattern applies to configure the kotlin source set convention HTH
j
@eskatos @Nikky Thanks for your help so far. This is the code I have so far:
Copy code
kotlin
sourceSets.create("integrationTest") {
    java.srcDir("src/integrationTest/kotlin")
    resources.srcDir("src/integrationTest/resources")
    compileClasspath += sourceSets["main"].output + configurations["testRuntimeClasspath"]
    runtimeClasspath += output + compileClasspath + sourceSets["test"].runtimeClasspath
}

task<Test>("integrationTest") {
    description = "Runs the integration tests"
    group = "verification"
    testClassesDirs = sourceSets["integrationTest"].output.classesDirs
    classpath = sourceSets["integrationTest"].runtimeClasspath
//    mustRunAfter(tasks["test"])
}
I took the code snippet from the Gradle website Paul showed me (I did have to upgrade to Gradle 4.10.2 though, and changed some things Since I have kotlin code the sources are located in
src/main/kotlin
and
src/integrationTest/kotlin
I also added
sourceSets["test"].runtimeClasspath
to the runtimeClassPath.
Right now everything compiles, but tests are not run when running the
integrationTest
task
e
you should set the
src/integrationTest/kotlin
src dir to the kotlin convention of the source set instead of on
java
1
j
I tried this with
kotlin.srcDir("...")
, but that didn’t work
e
see the second link I posted above
you need the
KotlinSourceSet
convention of your
integrationTest
source set
j
My bad. I’ll have a look
Copy code
kotlin
sourceSets {
    create("integrationTest") {
        withConvention(KotlinSourceSet::class) {
            kotlin.srcDir("src/integrationTest/kotlin")
            resources.srcDir("src/integrationTest/resources")
            compileClasspath += sourceSets["main"].output + configurations["testRuntimeClasspath"]
            runtimeClasspath += output + compileClasspath + sourceSets["test"].runtimeClasspath
        }
    }
}
That worked
The tests still did not run, until I changed the
@Test
annotation import from
import org.junit.jupiter.api.Test
to
Copy code
org.junit.Test
But I think I know how to solve that
e
👍
j
Copy code
task<Test>("integrationTest") {
    description = "Runs the integration tests"
    group = "verification"
    testClassesDirs = sourceSets["integrationTest"].output.classesDirs
    classpath = sourceSets["integrationTest"].runtimeClasspath
    mustRunAfter(tasks["test"])
    useJUnitPlatform()
}
Just add
useJunitPlatform()
I’ll answer my own question to SO
Would you like me to mention you guys?

https://media.giphy.com/media/3oz8xIsloV7zOmt81G/giphy.gif

e
My pleasure
I don’t mind not to be mentioned 🙂
j
👍