Hi all Ist there a way to easily unit-testing a kt...
# ktor
r
Hi all Ist there a way to easily unit-testing a ktor client plugin, without mocking the whole ktor client? I have a simple plugin that uses only
onResponse
b
Generally we use
testApplication
and install on the client there and test with the server. Here's an example from
CallIdTest
in the Ktor repository:
Copy code
@Test
fun testCallIdChainingFromCoroutineContext() = testApplication {
    val client = createClient {
        install(CallId)
    }

    install(ServerCallId) {
        generate { "call-id-1" }
    }
    routing {
        get("/1") {
            call.respond(client.get("2").bodyAsText())
        }
        get("2") {
            respondWithCallId()
        }
    }

    val response = client.get("/1").bodyAsText()
    assertEquals(response, "call-id-1:call-id-1:call-id-1")
}
a
With
testApplication
, the client and the server are connected, so they don't use a real network. Unfortunately, it's not possible to test a plugin in isolation.
r
Ok, thanks for your responses, will use
testApplication
then.
a
I would just extract the logic that is passed to onResponse and test only that logic… if possible
a
That would require mocking the
ApplicationCall
which depends on other interfaces.