Can I use Spock test in Koltin Multiplatform project? If so how to set in gradle.build.kts?

Viewed 302

I try to use Spock test framework in my Kotlin multiplatform project, but with fail, the gradle script:

plugins {
    val kotlinVersion = "1.4.30"
    kotlin("multiplatform") version kotlinVersion
    application
    kotlin("plugin.serialization") version kotlinVersion
    groovy // for spock
}
repositories {
    // ...
    maven("https://jitpack.io") // for spock
}
kotlin {
    jvm {withJava()}
    js {/*...*/}
    sourceSets {
        val commonMain by getting {
            dependencies {/*...*/}
        }
        val commonTest by getting {
            dependencies {
                implementation(kotlin("test-common"))
                implementation(kotlin("test-annotations-common"))
            }
        }
        val jvmMain by getting {
            dependencies {/*...*/}
        }
        val jvmTest by getting {
            dependencies {
                dependsOn(commonTest)
                implementation(kotlin("test"))
                implementation(kotlin("test-junit"))
                // implementation(kotlin("test-junit5"))
                // for spock
                implementation("org.spockframework.spock:spock-core:spock-1.3")
                implementation("org.codehaus.groovy:groovy-all:3.0.7")
            }
        }
        val jsMain by getting {
            dependencies {/*...*/}
        }
    }
}

I tried the spock-1.3 and spock-2.0-M4 with groovy-2.5 and Junit5, all failed with dependencies problems, even thought the test can run, it tells: Test events were not received, and more important:

I cannot create groovy source fold for test only.(The directory appreas like normal folder, not source or test folder)

How to configure the dependencies or gradle.build.kts file? Any help is highly appreciated!

1 Answers

I can now make Spock work with existed Ktor project with the code below added to my gradle.build.kts :

plugins {
    groovy
}

repositories {
    maven("https://jitpack.io")
}

dependencies {
    testImplementation("org.codehaus.groovy:groovy-all:2.4.15")
    testImplementation("org.spockframework.spock:spock-core:spock-1.3")
}

sourceSets {
    main {
        withConvention(GroovySourceSet::class) {
            groovy {
                setSrcDirs(listOf("src"))
            }
        }
    }
    test {
        withConvention(GroovySourceSet::class) {
            groovy {
                setSrcDirs(listOf("test"))
            }
        }
    }
}

tasks.test {
    useJUnitPlatform()
    testLogging {
        events("passed", "skipped", "failed")
    }
}

However, I cannot figure out the Koltin Multiplatform project with Spock test, I will post the script when I make it.

Related