I want to have a Gradle task that runs tests for m...
# gradle
p
I want to have a Gradle task that runs tests for my library using a specific JRE (8, the oldest one I want to support). Looking at toolchains for tasks, it should be fairly simple, but it's giving me this in runtime:
Copy code
Cannot invoke "org.gradle.api.file.FileCollection.getAsFileTree()" because the return value of "org.gradle.api.tasks.testing.Test.getTestClassesDirs()" is null
How can I fix it? CC @Adam S
t
Could you post the full stracktrace?
p
t
Could you show how you configure the task?
p
after this curly brace, I put the snippet from the docs, but replaced 17 with 8:
Copy code
tasks.register<Test>("testsOn8") {
    javaLauncher = javaToolchains.launcherFor {
        languageVersion = JavaLanguageVersion.of(8)
    }
}
a
For KMP try this instead:
Copy code
kotlin {
    targets.withType<KotlinJvmTarget>().configureEach {
        testRuns.register("${name}_testJdk8") {
            executionTask.configure {
                javaLauncher = javaToolchains.launcherFor {
                    languageVersion = JavaLanguageVersion.of(8)
                }
            }
        }
    }
}
p
@Adam S
Could not create domain object 'jvm_testJdk8' (KotlinJvmTestRun)
🤔 at the very bottom of the stack trace:
Copy code
Caused by: java.lang.NullPointerException: Cannot invoke "org.gradle.api.file.FileCollection.getFiles()" because "files" is null
I'll push it to PR so that you have context of the whole code base
t
do you need it in KMP project or JVM only?
p
KMP, it's for snakeyaml-engine-kmp See the PR with the change proposed by Adam, where CI will probably fail in a minute: https://github.com/krzema12/snakeyaml-engine-kmp/pull/203 (or if it doesn't fail, it means something's wrong with my local setup)
t
Something like this should work for Kotlin/JVM:
Copy code
tasks.register<Test>("testsOn8") {
    classpath = sourceSets.test.get().runtimeClasspath
    testClassesDirs = sourceSets.test.get().output.classesDirs
    workingDir = projectDir
    javaLauncher = javaToolchains.launcherFor {
        languageVersion = JavaLanguageVersion.of(8)
    }
    useJUnitPlatform()
}
probably you need to adapt @Adam Semenenko approach here with mine
174 Views