testApplication

fun testApplication(block: suspend ApplicationTestBuilder.() -> Unit): TestResult

Creates a test using TestApplication. To test a server Ktor application, do the following:

  1. Use testApplication function to set up a configured instance of a test application running locally.

  2. 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!")
}
}

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())
}

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.


fun testApplication(parentCoroutineContext: CoroutineContext = EmptyCoroutineContext, block: suspend ApplicationTestBuilder.() -> Unit): TestResult

Creates a test using TestApplication. To test a server Ktor application, do the following:

  1. Use testApplication function to set up a configured instance of a test application running locally.

  2. 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!")
}
}

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())
}

_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.