How do I enable "--enable-preview" for tests in Kotlin-based Gradle script ? I tried literally everything I could find online with https://stackoverflow.com/a/61849770/226895 being the closest to correct answer.
I still get following error on :test
task
org.gradle.api.internal.tasks.testing.TestSuiteExecutionException: Could not execute test class 'com.blabla.playground.AppTest'.
at org.gradle.api.internal.tasks.testing.SuiteTestClassProcessor.processTestClass(SuiteTestClassProcessor.java:53)
Caused by: java.lang.UnsupportedClassVersionError: Preview features are not enabled for com/blabla/playground/AppTest (class file version 58.65535). Try running with '--enable-preview'
at java.base/java.lang.ClassLoader.defineClass1(Native Method)
by script is
plugins {
java
application
}
repositories {
jcenter()
}
dependencies {
implementation("com.google.guava:guava:28.2-jre")
testImplementation("org.junit.jupiter:junit-jupiter-api:5.6.0")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.6.0")
}
application {
mainClassName = "com.blabla.playground.App"
}
java {
sourceCompatibility = JavaVersion.VERSION_14
targetCompatibility = JavaVersion.VERSION_14
}
tasks {
withType<Test>().all {
allJvmArgs.add("--enable-preview")
testLogging.showStandardStreams = true
testLogging.showExceptions = true
useJUnitPlatform {
}
}
withType<JavaExec>().all {
jvmArgs!!.add("--enable-preview")
}
withType<Wrapper>().all {
gradleVersion = "6.4.1"
distributionType = Wrapper.DistributionType.BIN
}
withType(JavaCompile::class.java).all {
options.compilerArgs.addAll(listOf("--enable-preview", "-Xlint:preview"))
}
jar {
manifest {
attributes("Main-Class" to "com.blabla.playground.App")
}
}
}
What am I missing?
It turned out that the way you are configuring the flag for Test
tasks does not really append the flag to allJvmArgs
.
I managed to made it work by configuring the Test
tasks like so :
withType<Test>().all {
jvmArgs("--enable-preview")
}
or
withType<Test>().all {
allJvmArgs = listOf("--enable-preview")
}
However the second option might not be preferable as it substitutes all JVM arguments for forked process (including arguments to define system properties, the minimum/maximum heap size, and the bootstrap classpath). The first option should be preferrable.