Browse Source

Exception response for MockRestServiceServer

Closes gh-1954
pull/23965/head
Rob Tompkins 7 years ago committed by Rossen Stoyanchev
parent
commit
cfb7777a07
  1. 12
      spring-test/src/main/java/org/springframework/test/web/client/response/MockRestResponseCreators.java
  2. 8
      spring-test/src/test/java/org/springframework/test/web/client/response/ResponseCreatorsTests.java

12
spring-test/src/main/java/org/springframework/test/web/client/response/MockRestResponseCreators.java

@ -16,6 +16,7 @@ @@ -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 { @@ -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;
};
}
}

8
spring-test/src/test/java/org/springframework/test/web/client/response/ResponseCreatorsTests.java

@ -16,6 +16,7 @@ @@ -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; @@ -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 { @@ -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);
}
}

Loading…
Cancel
Save