Hi, guys, I'm trying to write some tests, dealing ...
# ktor
d
Hi, guys, I'm trying to write some tests, dealing with json, and kotlinx.serialization, and I need to do:
Copy code
setBody(
                    Json.encodeToString(
                        CreateUpdateCustomer(
                            name = "name",
                            street = "street",
                            houseNumber = "houseNumber",
                            zip = "zip",
                            place = "place",
                            countryCode = "DE",
                            email = "<mailto:some@email.com|some@email.com>",
                            invoiceLanguage = "EN",
                        )
                    )
                )
to be able to do json Shouldn't it be just
Copy code
setBody(
                    CreateUpdateCustomer(
                            name = "name",
                            street = "street",
                            houseNumber = "houseNumber",
                            zip = "zip",
                            place = "place",
                            countryCode = "DE",
                            email = "<mailto:some@email.com|some@email.com>",
                            invoiceLanguage = "EN",
                        )
                )
a
The following test passes:
Copy code
import io.ktor.client.request.*
import io.ktor.client.statement.*
import io.ktor.http.*
import io.ktor.serialization.kotlinx.json.*
import io.ktor.server.application.*
import io.ktor.server.request.*
import io.ktor.server.response.*
import io.ktor.server.routing.*
import io.ktor.server.testing.*
import org.junit.Test
import kotlin.test.assertEquals

class SomeTest {
    @Test
    fun testPostCustomer() = testApplication {
        application {
            routing {
                post("/customer") {
                    println(call.receiveText()) // prints {"id":3,"name":"Jet","lastName":"Brains"}
                    call.respondText(status = HttpStatusCode.Created) { "Customer stored correctly" }
                }
            }
        }

        val client = createClient {
            install(io.ktor.client.plugins.ContentNegotiation) {
                json()
            }
        }
        val response = <http://client.post|client.post>("/customer") {
            contentType(ContentType.Application.Json)
            setBody(Customer(3, "Jet", "Brains"))
        }
        assertEquals("Customer stored correctly", response.bodyAsText())
        assertEquals(HttpStatusCode.Created, response.status)
    }
}

@kotlinx.serialization.Serializable
data class Customer(val id: Int, val name: String, val lastName: String)
What exact problem do you have with your test?