Search code examples
gradlegroovykotlin

Test classes in Groovy don't see test classes in Kotlin


I have gradle project with Kotlin plugin.

In my project I uses groovy and Spock for tests. One of utility classes used in tests in written in Kotlin and I put it to src/test/kotlin

I'm trying to use this class from groovy tests (Spock specification), I see that "compileTestKotlin" task runs first and compiles my utility class, but still "compileTestGroovy" fails, because it does not see it.

How can I fix this situation? How to add build/classes/kotlin/test/ to the compilation classpath of groovy tests?


Solution

  • The issue is that by default compileTestGroovy doesn't include build/classes/kotlin/test folder, so your Kotlin util class cannot be seen from Groovy tests.

    In order to fix it you can manually add Kotlin test sources to compileTestGroovy's classpath. Add the following to your build.gradle:

    compileTestGroovy.classpath += files(compileTestKotlin.destinationDir)
    // in more recent versions it must be
    compileTestGroovy.classpath += files(compileTestKotlin.destinationDirectory)
    

    If your build file is build.gradle.kts, add the following

    // make groovy test code depend on kotlin test code
    tasks.named<GroovyCompile>("compileTestGroovy") {
        classpath += files(tasks.compileTestKotlin)
    }