提交 cfb7777a 编写于 作者: R Rob Tompkins 提交者: Rossen Stoyanchev

Exception response for MockRestServiceServer

Closes gh-1954
上级 f638bfc6
......@@ -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;
};
}
}
......@@ -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);
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册