--------------------------------------------------...
# micronaut
c
------------------------------------------------------ Inspect raw response body in a test? ------------------------------------------------------ Hi again Micronaut experts, we're looking for further help with testing! We're testing error conditions for an API and want to check a 400 response is returned with a particular json payload when a mandatory parameter is missing. We can test the status code, but are getting a bit confused about how to test a json payload of the error response. The "happy day" tests we have all automatically convert the json to an object due
client.exchange(url, type)
forcing you to specify a type; and for the exception ones we are checking HttpClientResponseException , which doesn't seem to have body available I'd really like to just call the endpoint as if I was a client, and see the standard http response - where I can then check the response body as well as the status code - rather than this exception. Is there a way to do this, does anyone have any examples? Here's a simple representation of what our test looks like at the moment
Copy code
@MicronautTest
class SampleControllerTest {

    @Inject
    @field:Client( "/")
    lateinit var client: RxHttpClient

    @Test
    fun `should return bad request when no name is passed`() {
        val exception = assertThrows<HttpClientResponseException> {
            client.toBlocking()
                    .exchange("/test/hello", String::class.java)
        }

        assertThat(exception.status, equalTo(HttpStatus.BAD_REQUEST))

        //I want to test the response body contains { "errorReason" : "Missing query parameter: name"}
    }

}
See this branch for context https://github.com/stottpaul/micronaut-testcase/tree/test-raw-response-body Any help appreciated, thanks!