I'm new to Kotlin and trying Ktor 3.0.0-beta-1 to build API. I managed to build a route and it's working when I access localhost:port/api/path/to/route, but I'm having trouble testing it.
According to https://ktor.io/docs/3.0.0-beta-1/serialization-client.html I should be able to create client and test the route like this:
class ApplicationTest {
val client = HttpClient() {
install(ContentNegotiation) {
json()
}
}
@Test
fun testRoute() = testApplication {
application {}
val value: ReturnedValue = client.get("/api/path/to/route").body()
}
}
but have error Unresolved reference: body
I also tried
client.get("/api/path/to/route").apply {
assertEquals(ReturnedValue(), body<ReturnedValue>())
}
because there was an autogenerated test case
client.get("/").apply {
assertEquals(HttpStatusCode.OK, status)
assertEquals("Hello World!", bodyAsText())
}
I finally got it working, thank you commenters!
Since I'm new to Kotlin, it was hard to find what is and should be coming from where.
body, which @simon-jacobs pointed out, was missing from the article. I posted the question after ChatGPT and Google didn't help me finding where to import it from.testApplication {client...}, which was using default client. Following the article, I switched to trying to create one byHttpClient, which does not apply to testing. Thanks to @aleksei-tirman, I finally understood that I have to usecreateClientand that it is a dedicated way to create a client, with the context provided bytestApplication(so only callable inside).clientand customizablecreateClientare coming fromClientProvider→ApplicationTestBuilder→testApplication. It is easy to find out, only after knowing the answer.applyis a scope function which confused me by making methods, extension functions and normal functions indistinguishable. It makes the code shorter but does no actual work. When I read the code I have to take care of it, but no need for use by myself.