Riccardo Cardin
11/25/2023, 9:52 AMpost
route. The definition is the following:
fun Application.configureSerialization() {
install(ContentNegotiation) { json() }
}
fun Application.configureRouting() {
routing {
post("/portfolios") {
val dto = call.receive<CreatePortfolioDTO>()
call.response.header("Location", "/portfolios/1")
call.respond(HttpStatusCode.Created)
}
}
}
@Serializable data class CreatePortfolioDTO(val userId: String, val amount: Double)
The test I try to develop is the following:
class ApplicationTest {
@Test
fun testRoot() = testApplication {
application { configureRouting() }
val client = createClient { install(ContentNegotiation) { json() } }
val response =
client
.post {
url("/portfolios")
contentType(ContentType.Application.Json)
setBody(CreatePortfolioDTO("rcardin", 100.0))
}
.apply { assertEquals(status, HttpStatusCode.Created) }
}
}
I can’t understand why the test fails with the following message:
java.lang.AssertionError: expected:<415 Unsupported Media Type> but was:<201 Created>
Is there somebody that can help me understand what’s going on?
Thanks a lot 🙂Sam
11/25/2023, 9:59 AMassertEquals
. I suspect it should really be "Expected 201 but was 415".Sam
11/25/2023, 10:00 AMconfigureRouting()
but you didn't call configureSerialization()
. Maybe that's the problem?Riccardo Cardin
11/25/2023, 10:03 AMSam
11/25/2023, 10:03 AM