I'm trying to write unit tests for a post request but I'm having problems with the serialization: `...
s
I'm trying to write unit tests for a post request but I'm having problems with the serialization:
Copy code
Fail to serialize body. Content has type: class .models.NewWord, but OutgoingContent expected.
java.lang.IllegalStateException: Fail to serialize body. Content has type: class .models.NewWord, but OutgoingContent expected.
If you expect serialized body, please check that you have installed the corresponding plugin(like `ContentNegotiation`) and set `Content-Type` header.
My test according to the docs (2.0.0 btw) :
Copy code
testApplication {
                val c = createClient {
                    install(ContentNegotiation) {
                        json()
                    }
                }
                val r = c.post("/word") {
                    contentType(ContentType.Application.Json)
                    setBody(NewWord("a", "b", "c"))
                }
            }
I also tried header() instead of contentType() but doesn't work either
Copy code
header(HttpHeaders.ContentType, ContentType.Application.Json.toString())
the NewWord class is Serializable ofc. what am I missing?
r
Can you check that you import
ContentNegotiation
from
io.ktor.client.plugins
?
s
io.ktor.client.plugins.*
I mean, I have this import
can't find a specific ContentNegotiation object from the client stuff tho
r
my guess was that there are two
ContentNegotiation
plugins, for server and for client. It’s possible that you use one for server. The code will still compile, because there is
install
in the scope of
testApplication
too
s
yeah you're right
just had to add the
io.ktor:ktor-client-content-negotiation
dependency
👍 2
r
We may need to add
@DslMarker
to these builders. Thanks for your report
a
Here is another issue.