Skip to content

Latest commit

 

History

History
35 lines (29 loc) · 1.09 KB

Application-Testing.md

File metadata and controls

35 lines (29 loc) · 1.09 KB

Testing applications

  • Add ktor-test-host dependency to test scope
  • Create a JUnit test class and a test function
  • Use withTestApplication function to setup test environment for your Application
  • Use handleRequest function to send requests to your application and verify results

See full example of application testing in ktor-samples-testable

Sample application

fun Application.testableApplication() {
    intercept(ApplicationCallPipeline.Call) { call ->
        if (call.request.uri == "/")
            call.respondText("Test String")
    }
}

Application test

class ApplicationTest {
    @Test fun testRequest() = withTestApplication(Application::testableApplication) {
        with (handleRequest(HttpMethod.Get, "/")) {
            assertEquals(HttpStatusCode.OK, response.status())
            assertEquals("Test String", response.content)
        }
        with (handleRequest(HttpMethod.Get, "/index.html")) {
            assertFalse(requestHandled)
        }
    }
}