Hey, I'm trying to set up for an Android project, ...
# spek
j
Hey, I'm trying to set up for an Android project, following this https://www.spekframework.org/setup-android/ , but I would like to use KTS / build.gradle.kts files. It's basically working, but I can't figure out how to config the part "
Copy code
testLogging.events = ["passed", "skipped", "failed"]
in Kotlin KTS.. Anyone got a hint?
r
What does your build file look like? Kotlin does not have a list literal, so you have to use
listOf(..)
.
j
here's what it looks like:
import de.mannodermaus.gradle.plugins.junit5.junitPlatform
plugins {
id("com.android.library")
id("de.mannodermaus.android-junit5")
kotlin("android")
kotlin("android.extensions")
kotlin("kapt")
}
android {
compileSdkVersion(SdkVersions.compile_sdk_version)
defaultConfig {
versionCode = AppVersions.versionCode
versionName = AppVersions.versionName
minSdkVersion(SdkVersions.minSdkVersion)
targetSdkVersion( SdkVersions.targetSdkVersion)
testInstrumentationRunner = Dependencies.android_test_runner
testInstrumentationRunnerArgument("runnerBuilder", "de.mannodermaus.junit5.AndroidJUnit5Builder")
dataBinding {
isEnabled = true
}
lintOptions {
isAbortOnError = false
}
}
androidExtensions {
isExperimental = true
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
buildTypes {
getByName("release") {
isMinifyEnabled = false
proguardFiles(getDefaultProguardFile("proguard-android.txt"), "<http://proguard-rules.pro|proguard-rules.pro>")
}
}
sourceSets.getByName("main") {
java.srcDir("src/main/java")
java.srcDir("src/main/kotlin")
}
sourceSets.getByName("test") {
java.srcDir("src/test/java")
java.srcDir("src/test/kotlin")
}
//==================
testOptions {
junitPlatform {
filters {
includeEngines("spek2")
}
jacocoOptions {
// here goes all jacoco config, for example
html.enabled = true
xml.enabled = false
csv.enabled = false
}
}
}
}
the jacocoOptions part converted fine from groovy , but I can't seem to find how do migrate the testLogging.events = ["passed", "skipped", "failed"] part
r
What errors are you getting? Conversion should look like:
Copy code
unitTests.all {
    testLogging.events = listOf("passed", "skipped", "failed")
}
j
The error was that it wasn't able to create Groovy closure from Kotlin. Similar to Example 25 in this doc: https://docs.gradle.org/current/userguide/kotlin_dsl.html
I got what I wanted by doing this:
Copy code
tasks.withType<Test> {
        testLogging { 
            events("passed", "skipped", "failed")
        }
    }
unitTests.all expects a Groovy closure:
Copy code
public void all(final Closure<Test> configClosure) {
r
Ahh nice. Have you tried upgrading the android-junit 5 plugin? I'm pretty sure the example is using a very old version that might not be compatible with kotlin-dsl
j
i could not figure out how to get that Groovy closure from a
KotlinClosure1
it compiles, but failes with "value is null" very strange..
upgrading junit5 ? how?
r
not junit5 itself but
android-junit5
, versions are defined here: https://github.com/spekframework/spek/blob/2.x/samples/android/gradle.properties
j
Just updated, still seems to be the same if I'm not totally confused now
image.png
r
Sucks, just go with what you have previously then 🙂
j
yup, that's what I got 🙂
gradle+groovy can be such a nightmare sometimes
âž• 1
r
Indeed 🙂
j
thx for your response!
t
KotlinDSL has
closureOf<T> {}
method that you could use in this case: https://gradle.github.io/kotlin-dsl-docs/api/org.gradle.kotlin.dsl/kotlin.-any/closure-of.html
j
I tried that one and that didn't work. Did you try it?
t
yes, worked for me 🤔 You need to do
unitTests.all(closureOf { ... })