testApplication
Creates a test using TestApplication. To test a server Ktor application, do the following:
Use testApplication function to set up a configured instance of a test application running locally.
Use the HttpClient instance inside a test application to make a request to your server, receive a response, and make assertions.
Suppose, you have the following route that accepts GET requests made to the /
path and responds with a plain text response:
routing {
get("/") {
call.respondText("Hello, world!")
}
}
Content copied to clipboard
A test for this route will look as follows:
@Test
fun testRoot() = testApplication {
val response = client.get("/")
assertEquals(HttpStatusCode.OK, response.status)
assertEquals("Hello, world!", response.bodyAsText())
}
Content copied to clipboard
Note: If you have the application.conf
file in the resources
folder, testApplication loads all modules and properties specified in the configuration file automatically.
You can learn more from Testing.