https://kotlinlang.org logo
#kotest
Title
# kotest
m

Michael Vandendriessche

01/19/2022, 2:09 PM
Hi everyone, I've just started working on an app (which has many architecture and code quality problems) without any tests and after comparing test frameworks I think Kotest would be my favourite. I've been having some difficulties with setting it up though, mostly in the dependencies department. I wanted to know if the [getting started guide](https://kotest.io/docs/quickstart/) is still up to date and complete. Do I not need any other dependencies than these listed below for unit testing with kotest, with the kotest assertions?
Copy code
testImplementation 'io.kotest:kotest-runner-junit5-jvm:5.1.0'
testImplementation 'io.kotest:kotest-assertions-core-jvm:5.1.0'
Currently my test dependencies look like this (yes with all the comments... don't worry, I didn't commit it yet)
Copy code
//    testImplementation 'junit:junit:4.12'
    testImplementation 'io.kotest:kotest-runner-junit5-jvm:5.1.0'
    testImplementation 'io.kotest:kotest-assertions-core-jvm:5.1.0'
//    testImplementation 'io.kotest:kotest-assertions-json-jvm:5.1.0'
    testImplementation 'io.kotest.extensions:kotest-extensions-robolectric:0.5.0'
    implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version" // M: Why do I need this? ask in the kotlin kotest channel
//    testImplementation 'org.json:json:20180813' // M: needed for using JSONObject in unit tests, which is part of the android framework
//    testImplementation 'com.googlecode.json-simple:json-simple:1.1.1' // M: needed for using JSONObject in unit tests, which is part of the android framework
//    testImplementation 'org.robolectric:robolectric:4.6' // M: use kotest robolectric instead

//    testImplementation "de.mannodermaus.gradle.plugins:android-junit5:1.8.2.0"
    // (Required) Writing and executing Unit Tests on the JUnit Platform
//    testImplementation "org.junit.jupiter:junit-jupiter-api:5.8.2"
//    testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:5.8.2"

    // (Optional) If you need "Parameterized Tests"
//    testImplementation "org.junit.jupiter:junit-jupiter-params:5.8.2"

//    androidTestImplementation 'com.android.support.test:rules:1.0.2'
//    androidTestImplementation 'com.android.support.test:runner:1.0.2'
//    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
I'm using robolectric here because I wanted to test something with json but apparently that's part of the android platform and couldn't get it to work by including json dependencies separately. I had to include the kotlin-reflect library, however I'm not doing any reflection. Why do I need this dependency? Where is it using reflection? At this moment I'm having following error about androidx.
Copy code
This project uses AndroidX dependencies, but the 'android.useAndroidX' property is not enabled. Set this property to true in the gradle.properties file and retry.
The following AndroidX dependencies are detected: androidx.test:monitor:1.4.0, androidx.annotation:annotation:1.1.0
Where is androidx used? Why do I need to add this? I do not see any references to androidx in our codebase. I suppose I will add it but I want to understand it. I was wondering if I also should include the mannodermaus plugin for junit5 and the junit dependencies or those are covered by kotest already or are those not relevant for kotest? Most of my android knowledge dates from around 2015. If someone can shed more light on the things above, that would be great! I'm happy to be working on Android again! 😄
e

Emil Kantis

01/19/2022, 2:21 PM
What things from Robolectric do you want to use for JSON testing?
m

Michael Vandendriessche

01/19/2022, 2:22 PM
In the test I have right now, I just want to be able to make a
JSONObject
e

Emil Kantis

01/19/2022, 2:22 PM
can you make a small example?
👍 1
m

Michael Vandendriessche

01/19/2022, 2:23 PM
Copy code
describe("createFromJSON") {

    describe("with a valid json parameter"){
        val json = JSONObject(filterValidJson) //M: JSONObject is part of Android. unit test fails here.
        val createdFilter = Filter.createFromJSON(json)
        it("should create a filter with the same values"){
            assertSoftly(createdFilter){
                    id shouldBe 1
                    name shouldBe "filtername"
                    minimum shouldBe 1
                    maximum shouldBe 3
                    displayOrder shouldBe 10
            }
        }
    }
This is the only test I have right now (except a true is true test which worked before I made more changes)
This is the function
Copy code
@JvmStatic
@Throws(JSONException::class)
fun createFromJSON(json: JSONObject): Filter {
    return Filter(
        id = json.getInt(KEY_ID),
        name = json.getString(KEY_NAME),
        minimum = json.getInt(KEY_MINIMUM),
        maximum = json.getInt(KEY_MAXIMUM),
        displayOrder = json.getInt(KEY_DISPLAY_ORDER)
    )
}
I picked that one because it is small so expected it to be easy to use as example for further tests
This is the error I get at the jsonobject
Copy code
java.lang.RuntimeException: Stub!
	at org.json.JSONObject.<init>(JSONObject.java:120)
After I added
android.useAndroidX=true
to the gradle properties
e

Emil Kantis

01/19/2022, 2:40 PM
Gotcha! I’m a bit out of my depth here since I’m not working with Android.. hopefully this will be enough for someone else to pick up and give you some advice 🙂 I mostly picked up on the JSON parts, since I worked a lot with that, but for the server-side..
m

Michael Vandendriessche

01/19/2022, 2:42 PM
Thank you for taking a look at it 😄 I've also done some kotlin server side and it definitely made more sense there. Unfortunately we didnt know about kotest yet.
4 Views