Add a dependency to a custom source set in Kotlin DSL

I’m trying to migrate a Groovy Gradle build DSL based project. This project uses the testFixtures support for Java.
As Kotlin doesn’t seem to have support for testFixtures yet I have added the org.unbroken-dome.test-sets plugin that is adding the required custom source sets. I also had used this plugin in my Groovy based build script.

plugins {
  id ("org.unbroke-dome.test-sets") 
}

testSets {
  create ("componentTest")
  create("testFixtures")
}

Now I need to add a dependency. The componentTestImplementation configuration should have a dependency to the testFixtures classes.

With the Groovy DSL this would be just

dependencies { 
  componentTestImplementation(testFixtures(project))
}

For Kotlin I can’t figure out how this would have to look like.

This is the best solution that I was able to come up with. Don’t know if there are better ones.

testSets {
    val testFixtures=create("testFixtures")
    val componentTest=create("componentTest") {
        extendsFrom(testFixtures)
    }.apply {
        applyTestFixturesClasspath(this, testFixtures)
    }
}

fun applyTestFixturesClasspath (testSet: TestSetBase, testFixtures: TestSet) {
    testSet.sourceSet.compileClasspath += testFixtures.sourceSet.output
    testSet.sourceSet.runtimeClasspath += testFixtures.sourceSet.output
}

As Kotlin doesn’t seem to have support for testFixtures yet I have added the org.unbroken-dome.test-sets plugin that is adding the required custom source sets.

Why do you think so?
The Kotlin DSL supports exactly the same things as the Groovy DSL.
They are just different front-end languages, but both are feature-complete and equally supported.

Also, if you want to have different kinds of tests which I guess from the name “componentTest”, I’d suggest you have a look at the built-in JVM Testsuites plugin, which allows you to easily declare multiple “test sets”.

With the Groovy DSL this would be just

dependencies { 
 componentTestImplementation(testFixtures(project))
}

For Kotlin I can’t figure out how this would have to look like.

Either the ugly stringy-version:

dependencies { 
  "componentTestImplementation"(testFixtures(project))
}

or the imho nicer version:

val componentTestImplementation by configurations
dependencies { 
  componentTestImplementation(testFixtures(project))
}

Or if you use the JVM test suites plugin as advised above,

testing {
  suites {
    val componentTest by registering(JvmTestSuite::class) {
      [...]
      dependencies {
        implementation(project.dependencies.testFixtures(project))
      }
    }
  }
}

Thanks for pointing me to this. I’m just starting with Kotlin and probably wasn’t aware of what the JvmTestSuite can do for me. I’ll try your solution

Updated my Kotlin DSL based build script with your suggestions. This is a really neat solution. Thanks !

plugins {
    id ("jvm-test-suite")
    id ("java-test-fixtures")

    id("org.springframework.boot") version "2.7.2"
    id("io.spring.dependency-management") version "1.0.12.RELEASE"

    kotlin("jvm") version "1.6.21"
    kotlin("plugin.spring") version "1.6.21"
    kotlin("plugin.jpa") version "1.6.21"
    kotlin("plugin.allopen") version "1.6.21" // see https://kotlinlang.org/docs/all-open-plugin.html
}

// omitted code...

testing {
    suites {
        val test by getting(JvmTestSuite::class) {
            useJUnitJupiter()
            dependencies  {
                implementation("org.springframework.boot:spring-boot-starter-test")
            }
        }
        val componentTest by registering(JvmTestSuite::class) {
            dependencies  {
                implementation(project)
                implementation(project.dependencies.testFixtures(project))
                implementation("org.springframework.boot:spring-boot-starter-test")
                implementation("org.springframework.boot:spring-boot-starter-data-jpa")
            }

            targets {
                all {
                    testTask.configure {
                        shouldRunAfter(test)
                    }
                }
            }
        }
    }
}

tasks.named("check") {
    dependsOn(testing.suites.named("componentTest"))
}