Exception response for MockRestServiceServer

Closes gh-1954
This commit is contained in:
Rob Tompkins 2018-09-09 20:31:07 -04:00 committed by Rossen Stoyanchev
parent f638bfc6a9
commit cfb7777a07
2 changed files with 20 additions and 0 deletions

View File

@ -16,6 +16,7 @@
package org.springframework.test.web.client.response;
import java.io.IOException;
import java.net.URI;
import org.springframework.core.io.Resource;
@ -116,4 +117,15 @@ public abstract class MockRestResponseCreators {
return new DefaultResponseCreator(status);
}
/**
* {@code ResponseCreator} with an internal application {@code IOException}. For example,
* one could use this to simulate a {@code SocketTimeoutException}.
* @param e the {@code Exception} to be thrown at HTTP call time.
*/
public static ResponseCreator withException(IOException e) {
return request -> {
throw e;
};
}
}

View File

@ -16,6 +16,7 @@
package org.springframework.test.web.client.response;
import java.net.SocketTimeoutException;
import java.net.URI;
import org.junit.jupiter.api.Test;
@ -23,6 +24,7 @@ import org.junit.jupiter.api.Test;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.mock.http.client.MockClientHttpResponse;
import org.springframework.test.web.client.ResponseCreator;
import org.springframework.util.StreamUtils;
import static org.assertj.core.api.Assertions.assertThat;
@ -124,4 +126,10 @@ public class ResponseCreatorsTests {
assertThat(StreamUtils.copyToByteArray(response.getBody()).length).isEqualTo(0);
}
@Test(expected = SocketTimeoutException.class)
public void withException() throws Exception {
ResponseCreator responseCreator = MockRestResponseCreators.withException(new SocketTimeoutException());
responseCreator.createResponse(null);
}
}