While testing a RestClient-Implementation I want to simulate a RestClientException that may be thrown by some RestTemplate-methods in that implementation f.e. the delete-method:
You can test throwing runtime exceptions from the MockRestServiceServer
, although this class, as of Spring 5.0.0.RC4, is not designed for it (which means it may not work for more complex use cases):
RestTemplate yourApi;
MockRestServiceServer server = MockRestServiceServer.createServer(yourApi);
server.expect(requestTo("http://..."))
.andRespond((response) -> { throw new ResourceAccessException(
new ConnectException("Connection reset")); });
It seems to work in tests:
RestTemplate
call,I wasn't able to expect two consecutive exceptions; the MockRestSeriviceServer
(more concrete, the SimpleRequestExpectationManager
) throws IllegalStateException
on replaying the second expectation.