Changing a test source-set's toolchain

I have a Java 8 module which uses JDK8 via toolchains.
This module also has a test source set with JUnit unit tests in which I’d like to use a library ( whose latest version only supports JDK11 or higher.
Is it possible to change the test source set’s toolchain to run with JDK 11?
Changing the test task’s toolchain as described in Toolchains for JVM projects does not do the trick it seems.

Changing only the test tasks toolchain is too late, as it is not a testRuntimeOnly dependency, but a testImplementation dependency.

The compiler already needs to be compatible.

If you change the javaCompiler of the compileTestJava task to Java 11, the dependency can be resolved and compilation can succeed. You of course need the configuration on the test task too, as it is not a testCompileOnly dependency.

Thanks @Vampire! Very clear now.

tasks.named<JavaCompile>("compileTestJava") {
    javaCompiler.set(javaToolchains.compilerFor {

tasks.named<Test>("test") {
    javaLauncher.set(javaToolchains.launcherFor {
1 Like