Nikita Zhuravlev opened SPR-14458 and commented

Since MockRestServiceServer simply replaces RestTemplate requestFactory and requestFactory serves as the holder of HttpClient and timeouts settings you will get irrelevant test results in case you'll make your own realisation of ResponseCreator with timeout simulation.


Issue Links: - #20866 Limit thread synchronization in AbstractRequestExpectationManager

1 votes, 4 watchers

Comment From: spring-projects-issues

Rossen Stoyanchev commented

I'm sorry but I'm not following what you have in mind. The MockRestServerServer does configure the RestTemplate with a custom ClientHttpRequestFactory but that factory provides pre-configured mock responses. I am not sure how timeout settings can apply to mock responses.

Comment From: spring-projects-issues

Nikita Zhuravlev commented

https://gist.github.com/NZhuravlev/bb85387489bb482f47868fa0cf58174c Here is how I expected this to work before exploring the underlying logic.

I wanted to test that in situation when 3d party api is unstable I would still get response. As far as I see there is a possibility for that, for example here is the library supporting this type of testing: http://wiremock.org/docs/simulating-faults/

Comment From: spring-projects-issues

Rossen Stoyanchev commented

Okay thanks for clarifying. I've modified the title accordingly.

Comment From: spring-projects-issues

Rossen Stoyanchev commented

I'm wondering if this is a duplicate of #20866? In other words were you looking to make it possible to block inside a response creator, or for a built-in, delayed ResponseCreator?

Comment From: spring-projects-issues

Rob Tompkins commented

Suppose that I wanted to do something like the following:

RestTemplate restTemplate = new RestTemplate();
restTemplate.setInterceptors(Collections.singletonList(new MyWhackoPersonalInterceptor()));
SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
requestFactory.setConnectTimeout(1);
requestFactory.setReadTimeout(1);
restTemplate.setRequestFactory(requestFactory);
MockRestServiceServer mockServer = MockRestServiceServer.bindTo(restTemplate)
    .build();
mockServer.expect(requestTo("/something")).andRespond(
    request -> {
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {}
        return new MockClientHttpResponse("{}".getBytes(), HttpStatus.OK);
    }
);
restTemplate.getForEntity("/something", String.class);

so that I could see how my interceptor behaved during a timeout.

Note. I'd be happy to work on this if you don't Rossen Stoyanchev

Comment From: spring-projects-issues

Rob Tompkins commented

And I don't think that this is a duplicate of #20866 as I think the goal here is to accommodate for making some changes to the request factory so that we can force a timeout exception to occur. Thoughts?

Comment From: spring-projects-issues

Rob Tompkins commented

https://github.com/spring-projects/spring-framework/pull/1954

Comment From: christiangoudreau

Is there any news on this?

Comment From: duclm2609

Nearly 4 years later, any progress here? How do I test my interceptor behavior with MockRestServiceServer using @RestClientTest annotation?

Comment From: rstoyanchev

Superseded by #1954.

Comment From: wn9237

Hi, any updates on this?

Comment From: bclozel

@wn9237 read the comment just above yours. This has been implemented.