diff --git a/spring-test/src/main/java/org/springframework/test/web/client/AbstractRequestExpectationManager.java b/spring-test/src/main/java/org/springframework/test/web/client/AbstractRequestExpectationManager.java index 242ab77c59..d990832133 100644 --- a/spring-test/src/main/java/org/springframework/test/web/client/AbstractRequestExpectationManager.java +++ b/spring-test/src/main/java/org/springframework/test/web/client/AbstractRequestExpectationManager.java @@ -48,12 +48,10 @@ public abstract class AbstractRequestExpectationManager implements RequestExpect } - @Override public List getExpectations() { return this.expectations; } - @Override public List getRequests() { return this.requests; } diff --git a/spring-test/src/main/java/org/springframework/test/web/client/MockRestServiceServer.java b/spring-test/src/main/java/org/springframework/test/web/client/MockRestServiceServer.java index 0c31c74cb4..71bae3d573 100644 --- a/spring-test/src/main/java/org/springframework/test/web/client/MockRestServiceServer.java +++ b/spring-test/src/main/java/org/springframework/test/web/client/MockRestServiceServer.java @@ -18,7 +18,6 @@ package org.springframework.test.web.client; import java.io.IOException; import java.net.URI; -import java.util.List; import org.springframework.http.HttpMethod; import org.springframework.http.client.AsyncClientHttpRequest; @@ -96,69 +95,25 @@ import org.springframework.web.client.support.RestGatewaySupport; */ public class MockRestServiceServer { - private RequestExpectationManager expectationManager = new OrderedRequestExpectationManager(); + private final RequestExpectationManager expectationManager; /** * Private constructor. - * @see #createServer(RestTemplate) - * @see #createServer(RestGatewaySupport) + * See static builder methods and {@code createServer} shortcut methods. */ private MockRestServiceServer() { - } - - - /** - * Create a {@code MockRestServiceServer} and set up the given - * {@code RestTemplate} with a mock {@link ClientHttpRequestFactory}. - * @param restTemplate the RestTemplate to set up for mock testing - * @return the created mock server - */ - public static MockRestServiceServer createServer(RestTemplate restTemplate) { - Assert.notNull(restTemplate, "'restTemplate' must not be null"); - MockRestServiceServer mockServer = new MockRestServiceServer(); - MockClientHttpRequestFactory factory = mockServer.new MockClientHttpRequestFactory(); - restTemplate.setRequestFactory(factory); - return mockServer; + this.expectationManager = new SimpleRequestExpectationManager(); } /** - * Create a {@code MockRestServiceServer} and set up the given - * {@code AsyRestTemplate} with a mock {@link AsyncClientHttpRequestFactory}. - * @param asyncRestTemplate the AsyncRestTemplate to set up for mock testing - * @return the created mock server + * Private constructor with {@code RequestExpectationManager}. + * See static builder methods and {@code createServer} shortcut methods. */ - public static MockRestServiceServer createServer(AsyncRestTemplate asyncRestTemplate) { - Assert.notNull(asyncRestTemplate, "'asyncRestTemplate' must not be null"); - MockRestServiceServer mockServer = new MockRestServiceServer(); - MockClientHttpRequestFactory factory = mockServer.new MockClientHttpRequestFactory(); - asyncRestTemplate.setAsyncRequestFactory(factory); - return mockServer; + private MockRestServiceServer(RequestExpectationManager expectationManager) { + this.expectationManager = expectationManager; } - /** - * Create a {@code MockRestServiceServer} and set up the given - * {@code RestGatewaySupport} with a mock {@link ClientHttpRequestFactory}. - * @param restGateway the REST gateway to set up for mock testing - * @return the created mock server - */ - public static MockRestServiceServer createServer(RestGatewaySupport restGateway) { - Assert.notNull(restGateway, "'gatewaySupport' must not be null"); - return createServer(restGateway.getRestTemplate()); - } - - - /** - * When this option is set, the order in which requests are executed does not - * need to match the order in which expected requests are declared. - */ - public MockRestServiceServer setIgnoreRequestOrder() { - String message = "Cannot switch to unordered mode after actual requests are made."; - Assert.state(this.expectationManager.getRequests().isEmpty(), message); - List expectations = this.expectationManager.getExpectations(); - this.expectationManager = new UnorderedRequestExpectationManager(expectations); - return this; - } /** * Set up a new HTTP request expectation. The returned {@link ResponseActions} @@ -183,6 +138,131 @@ public class MockRestServiceServer { } + /** + * Build a {@code MockRestServiceServer} with a {@code RestTemplate}. + * @since 4.3 + */ + public static MockRestServiceServerBuilder restTemplate(RestTemplate restTemplate) { + return new DefaultBuilder(restTemplate); + } + + /** + * Build a {@code MockRestServiceServer} with an {@code AsyncRestTemplate}. + * @since 4.3 + */ + public static MockRestServiceServerBuilder asyncRestTemplate(AsyncRestTemplate asyncRestTemplate) { + return new DefaultBuilder(asyncRestTemplate); + } + + /** + * Build a {@code MockRestServiceServer} with a {@code RestGateway}. + * @since 4.3 + */ + public static MockRestServiceServerBuilder restGateway(RestGatewaySupport restGateway) { + Assert.notNull(restGateway, "'gatewaySupport' must not be null"); + return new DefaultBuilder(restGateway.getRestTemplate()); + } + + + /** + * A shortcut for {@code restTemplate(restTemplate).build()}. + * @param restTemplate the RestTemplate to set up for mock testing + * @return the mock server + */ + public static MockRestServiceServer createServer(RestTemplate restTemplate) { + return restTemplate(restTemplate).build(); + } + + /** + * A shortcut for {@code asyncRestTemplate(asyncRestTemplate).build()}. + * @param asyncRestTemplate the AsyncRestTemplate to set up for mock testing + * @return the created mock server + */ + public static MockRestServiceServer createServer(AsyncRestTemplate asyncRestTemplate) { + return asyncRestTemplate(asyncRestTemplate).build(); + } + + /** + * A shortcut for {@code restGateway(restGateway).build()}. + * @param restGateway the REST gateway to set up for mock testing + * @return the created mock server + */ + public static MockRestServiceServer createServer(RestGatewaySupport restGateway) { + return restGateway(restGateway).build(); + } + + + + /** + * Builder to create a {@code MockRestServiceServer}. + + */ + public interface MockRestServiceServerBuilder { + + /** + * When this option is set, requests can be executed in any order, i.e. + * not matching the order in which expected requests are declared. + */ + MockRestServiceServerBuilder ignoreExpectOrder(); + + /** + * Build the {@code MockRestServiceServer} and setting up the underlying + * {@code RestTemplate} or {@code AsyncRestTemplate} with a + * {@link ClientHttpRequestFactory} that creates mock requests. + */ + MockRestServiceServer build(); + + } + + private static class DefaultBuilder implements MockRestServiceServerBuilder { + + private final RestTemplate restTemplate; + + private final AsyncRestTemplate asyncRestTemplate; + + private boolean ignoreExpectOrder; + + + public DefaultBuilder(RestTemplate restTemplate) { + Assert.notNull(restTemplate, "'restTemplate' must not be null"); + this.restTemplate = restTemplate; + this.asyncRestTemplate = null; + } + + public DefaultBuilder(AsyncRestTemplate asyncRestTemplate) { + Assert.notNull(asyncRestTemplate, "'asyncRestTemplate' must not be null"); + this.restTemplate = null; + this.asyncRestTemplate = asyncRestTemplate; + } + + + @Override + public MockRestServiceServerBuilder ignoreExpectOrder() { + this.ignoreExpectOrder = true; + return this; + } + + + @Override + public MockRestServiceServer build() { + + MockRestServiceServer server = (this.ignoreExpectOrder ? + new MockRestServiceServer(new UnorderedRequestExpectationManager()) : + new MockRestServiceServer()); + + MockClientHttpRequestFactory factory = server.new MockClientHttpRequestFactory(); + if (this.restTemplate != null) { + this.restTemplate.setRequestFactory(factory); + } + if (this.asyncRestTemplate != null) { + this.asyncRestTemplate.setAsyncRequestFactory(factory); + } + + return server; + } + } + + /** * Mock ClientHttpRequestFactory that creates requests by iterating * over the list of expected {@link DefaultResponseActions}'s. diff --git a/spring-test/src/main/java/org/springframework/test/web/client/RequestExpectationManager.java b/spring-test/src/main/java/org/springframework/test/web/client/RequestExpectationManager.java index e919a20e02..d15b8094ee 100644 --- a/spring-test/src/main/java/org/springframework/test/web/client/RequestExpectationManager.java +++ b/spring-test/src/main/java/org/springframework/test/web/client/RequestExpectationManager.java @@ -16,7 +16,6 @@ package org.springframework.test.web.client; import java.io.IOException; -import java.util.List; import org.springframework.http.client.ClientHttpRequest; import org.springframework.http.client.ClientHttpResponse; @@ -31,16 +30,6 @@ import org.springframework.http.client.ClientHttpResponse; */ public interface RequestExpectationManager { - /** - * Return the list of declared request expectations. - */ - List getExpectations(); - - /** - * Return the list of actual requests. - */ - List getRequests(); - /** * Set up a new request expectation. The returned {@link ResponseActions} is * used to add more expectations and define a response. diff --git a/spring-test/src/main/java/org/springframework/test/web/client/OrderedRequestExpectationManager.java b/spring-test/src/main/java/org/springframework/test/web/client/SimpleRequestExpectationManager.java similarity index 96% rename from spring-test/src/main/java/org/springframework/test/web/client/OrderedRequestExpectationManager.java rename to spring-test/src/main/java/org/springframework/test/web/client/SimpleRequestExpectationManager.java index 12437e4a9a..7a58679778 100644 --- a/spring-test/src/main/java/org/springframework/test/web/client/OrderedRequestExpectationManager.java +++ b/spring-test/src/main/java/org/springframework/test/web/client/SimpleRequestExpectationManager.java @@ -30,7 +30,7 @@ import org.springframework.http.client.ClientHttpResponse; * @author Rossen Stoyanchev * @since 4.3 */ -public class OrderedRequestExpectationManager extends AbstractRequestExpectationManager { +public class SimpleRequestExpectationManager extends AbstractRequestExpectationManager { private Iterator iterator; diff --git a/spring-test/src/test/java/org/springframework/test/web/client/OrderedRequestExpectationManagerTests.java b/spring-test/src/test/java/org/springframework/test/web/client/OrderedRequestExpectationManagerTests.java index 1a578d3888..755e4204ad 100644 --- a/spring-test/src/test/java/org/springframework/test/web/client/OrderedRequestExpectationManagerTests.java +++ b/spring-test/src/test/java/org/springframework/test/web/client/OrderedRequestExpectationManagerTests.java @@ -36,7 +36,7 @@ import static org.springframework.test.web.client.response.MockRestResponseCreat */ public class OrderedRequestExpectationManagerTests { - private OrderedRequestExpectationManager manager = new OrderedRequestExpectationManager(); + private SimpleRequestExpectationManager manager = new SimpleRequestExpectationManager(); @Test