Hi, why am i getting 404 in test ? ```withTestApplication { handleRequest(HttpMethod.Get, "/hel...
e
Hi, why am i getting 404 in test ?
Copy code
withTestApplication {
    handleRequest(HttpMethod.Get, "/hello").apply {
        assertEquals(HttpStatusCode.OK, response.status())
    }
}
a
Could you please share your routing code?
e
Copy code
route("/hello") {
    get {
        call.respondText(text = "Hello", status = HttpStatusCode.OK)
    }
}
a
The following test passes:
Copy code
@Test
fun test() {
    withTestApplication {
        application.routing {
            route("/hello") {
                get {
                    call.respondText(text = "Hello", status = HttpStatusCode.OK)
                }
            }
        }

        handleRequest(HttpMethod.Get, "/hello").apply {
            assertEquals(HttpStatusCode.OK, response.status())
        }
    }
}
e
should we declare all routes before the test?
a
You need to apply them to a test application.
For example:
Copy code
class Test {
    @Test
    fun test() {
        withTestApplication {
            application.myRouting()

            handleRequest(HttpMethod.Get, "/hello").apply {
                assertEquals(HttpStatusCode.OK, response.status())
            }
        }
    }
}

fun Application.myRouting() {
    routing {
        route("/hello") {
            get {
                call.respondText(text = "Hello", status = HttpStatusCode.OK)
            }
        }
    }
}
This way you can apply routing to both real and test applications.
e
Thx 🙂
Exception in thread "main" io.ktor.application.DuplicateApplicationFeatureException: Conflicting application feature is already installed with the same key as
ContentNegotiation
i think this 404 occurs because of this error
because i did what you said
a
Please share a complete code for your test or a sample project.