When you set a custom request factory for "production" use you still might want to test with a MockRestServiceServer, but the mock request factory is not available once you have set the request factory. Probably needs some special casing in the MockServerRestTemplateCustomizer.
@dsyer I don't understand what you did here. Can you provide an example of how you made it break?
I needed a BufferingClientHttpRequestFactory and the requestFactory() method doesn't let me do that, so I needed to use a customizer. The customizer that adds the mock request factory then gets overwritten by the user's request factory.
Here's a sample:
this.rest = restTemplateBuilder.additionalCustomizers((restTemplate) -> {
restTemplate.setErrorHandler(...);
}, (restTemplate) -> {
restTemplate.setRequestFactory(new BufferingClientHttpRequestFactory(
new HttpComponentsClientHttpRequestFactory()));
}).messageConverters(new ErrorLoggingMappingJackson2HttpMessageConverter())
.build();
Thanks. Why not do this instead:
restTemplate.setRequestFactory(new BufferingClientHttpRequestFactory(
restTemplate.getRequestFactory()));
Because the buffering wrapper is removed by the builder (another bug or possibly just exposing this one). There's that bit of reflection Hackett in the builder that strips off the wrapper, remember?
I remember, but I don't think that's a problem here.
The builder potentially removing the wrapper has already happened by the time any template customisers are called. The wrapper removal is only a problem when you've configured the request factory on the builder rather than directly on the template that it's built.
This doesn't seem to have gone anywhere. Did you try my suggestion? As I said above, I think it should work.
If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed.
I didn't have time yet. Thanks for the reminder.
Hi,
In this particular case, When you use the restTemplate.getRequestFactory() method, you actually get a RequestFactory with all ClientHttpRequestInterceptorapplied. In my case, I use a BufferingClientHttpRequestFactory + a logging interceptor. My guess is that's why the buffering request factory does not correctly wrap the mock request factory.
Anyway, I found a workaround that seems to be working well: instead of using restTemplate.getRequestFactory(), I access the field directly via ReflectionTestUtils.
Here is an example:
mockServer = MockRestServiceServer.bindTo(restTemplate).build();
final ClientHttpRequestFactory requestFactory = (ClientHttpRequestFactory)ReflectionTestUtils.getField(restTemplate, "requestFactory");
restTemplate.setRequestFactory(new BufferingClientHttpRequestFactory(requestFactory));
Closing due to lack of requested feedback. If you would like us to look at this issue, please provide the requested information and we will re-open the issue.
Most helpful comment
Hi,
In this particular case, When you use the
restTemplate.getRequestFactory()method, you actually get aRequestFactorywith allClientHttpRequestInterceptorapplied. In my case, I use aBufferingClientHttpRequestFactory+ a logging interceptor. My guess is that's why the buffering request factory does not correctly wrap the mock request factory.Anyway, I found a workaround that seems to be working well: instead of using
restTemplate.getRequestFactory(), I access the field directly viaReflectionTestUtils.Here is an example: