Integration tests with Gradle Kotlin DSL

前端 未结 4 961
梦谈多话
梦谈多话 2020-12-31 06:07

I\'m using this blog post to configure integration tests for a Spring Boot project, but I\'m pretty stuck on declaring the source sets. I also found this post on StackOverfl

4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-31 06:22

    I was finally able to figure it out thanks to some help on the Kotlin Slack channel. First of all I had to upgrade to Gradle version 4.10.2.

    For more info have a look at these two pages from Gradle:

    • https://docs.gradle.org/release-nightly/userguide/organizing_gradle_projects.html#sec:separate_test_type_source_files
    • https://docs.gradle.org/release-nightly/userguide/organizing_gradle_projects.html#sec:separate_test_type_source_files

    Then I just had to create the sourceSets for the integrationTests

    sourceSets {
        create("integrationTest") {
                kotlin.srcDir("src/integrationTest/kotlin")
                resources.srcDir("src/integrationTest/resources")
                compileClasspath += sourceSets["main"].output + configurations["testRuntimeClasspath"]
                runtimeClasspath += output + compileClasspath + sourceSets["test"].runtimeClasspath
        }
    }
    

    This would work just fine for Java, but since I'm working with Kotlin I had to add an extra withConvention wrapper

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

    In the docs they only put runtimeClasspath += output + compileClasspath, but I added sourceSets["test"].runtimeClasspath so I can directly use the test dependencies instead of declaring new dependencies for the integrationTest task.

    Once the sourceSets were created it was a matter of declaring a new task

    task("integrationTest") {
        description = "Runs the integration tests"
        group = "verification"
        testClassesDirs = sourceSets["integrationTest"].output.classesDirs
        classpath = sourceSets["integrationTest"].runtimeClasspath
        mustRunAfter(tasks["test"])
    }
    

    After this the tests still didn't run, but that was because I'm using JUnit4. So I just had to add useJUnitPlatform() which makes this the final code

    task("integrationTest") {
        description = "Runs the integration tests"
        group = "verification"
        testClassesDirs = sourceSets["integrationTest"].output.classesDirs
        classpath = sourceSets["integrationTest"].runtimeClasspath
        mustRunAfter(tasks["test"])
        useJUnitPlatform()
    }
    

提交回复
热议问题