dimanche 31 juillet 2016

How to test code that calls top level functions in Kotlin?

I am very new to Kotlin.

I have a class that calls a top level function (which makes a http call). I am trying to write unit tests for my class without having it go out to the network.

Is there a way to mock/powermock/intercept the call from my class to the Kotlin top level function?

class MyClass {
    fun someMethod() {
        // do some stuff
        "http://ift.tt/2akR97n".httpGet(asList("someKey" to "someValue")).responseString { (request, response, result) ->
            // some processing code
        }
    }
}

It is using the kittinunf/Fuel library for the httpGet call.

It adds a top level function to String that ultimately calls a companion object function in Fuel (Fuel.get()).

The unit test needs to intercept the call to httpGet so that I can return a json string for the test.

Aucun commentaire:

Enregistrer un commentaire