I've created a Spring Boot v4.0.0-M3 app that reproduces an issue with the new TestRestClient. To reproduce the issue
- Clone the app, be sure to use the
rest-test-client-spring-v4.0.0-m3branch - Run the tests in ItemControllerTests
This class has two tests
parseResponseAsApiErrorResponsecalls an endpoint and parses the response to aApiErrorResponseparseResponseAsStringcalls an endpoint, parses the response body as aStringthen converts that to anApiErrorResponseviaobjectMapper.readValue(responseBody, ApiErrorResponse.class)
The first test fails, but the second one passes, because only the second test calls the custom deserializer ApiErrorResponseDeserializer.
Why is the custom serializer ignored by the first test?
Comment From: wilkinsona
You've created the RestTestClient in your own configuration and haven't configured it to use the JsonMapper from the context. As such, it doesn't know anything about your custom JSON configuration. To get it to work, it would have to be set up with a JacksonJsonHttpMessageConverter that's using the context's JsonMapper.
Built-in support for RestTestClient is coming in 4.0.0-RC1, although I think it may be missing this customisation too. I'll re-open #47335 so that we can double-check.