When defining a route that runs into an exception ...
# ktor
r
When defining a route that runs into an exception ktor will return an internal server error (500) by default.
Copy code
fun Application.configureRouting() {
    routing {
        get("/") {
            error("Something went wrong")
        }
    }
}
But when using this in a test, the test application will not return a 500 but throw the exception instead
Copy code
class ApplicationTest {
    @Test
    fun testRoot() = testApplication {
        application {
            configureRouting()
        }
        
        val response = client.get("/") // java.lang.IllegalStateException: Something went wrong
        assertEquals(HttpStatusCode.InternalServerError, response.status)
    }
}
How do I test this behavior when I rely on the default implementation of ktor?
r
there is a poorly documented feature for this. you can add
ktor.test.throwOnException=false
to your config. For example like this in
Copy code
testApplication {
    environment {
        config = MapApplicationConfig("ktor.test.throwOnException" to "false")
    }
    ....
}
👍 1
r
Thank you
I wonder why this is not the default. I would expect the test application to have the same behavior as the application in this regard
👍 1
r
It’s more convenient to get exceptions in tests if your app does something unexpected, imo.