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 f91b1c77e1..ed456417dd 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 @@ -19,6 +19,7 @@ package org.springframework.test.web.client; import java.io.IOException; import java.net.URI; import java.util.Collection; +import java.util.Collections; import java.util.LinkedHashSet; import java.util.LinkedList; import java.util.List; @@ -49,26 +50,32 @@ public abstract class AbstractRequestExpectationManager implements RequestExpect private final List requests = new LinkedList<>(); + /** + * Return a read-only list of the expectations. + */ protected List getExpectations() { - return this.expectations; + return Collections.unmodifiableList(this.expectations); } + /** + * Return a read-only list of requests executed so far. + */ protected List getRequests() { - return this.requests; + return Collections.unmodifiableList(this.requests); } @Override public ResponseActions expectRequest(ExpectedCount count, RequestMatcher matcher) { - Assert.state(getRequests().isEmpty(), "Cannot add more expectations after actual requests are made"); + Assert.state(this.requests.isEmpty(), "Cannot add more expectations after actual requests are made"); RequestExpectation expectation = new DefaultRequestExpectation(count, matcher); - getExpectations().add(expectation); + this.expectations.add(expectation); return expectation; } @Override public ClientHttpResponse validateRequest(ClientHttpRequest request) throws IOException { - List requests = getRequests(); + List requests = this.requests; synchronized (requests) { if (requests.isEmpty()) { afterExpectationsDeclared(); @@ -80,25 +87,26 @@ public abstract class AbstractRequestExpectationManager implements RequestExpect } /** - * Invoked after the phase of declaring expected requests is over. This is - * detected from {@link #validateRequest} on the first actual request. + * Invoked at the time of the first actual request, which effectively means + * the expectations declaration phase is over. */ protected void afterExpectationsDeclared() { } /** - * Subclasses must implement the actual validation of the request - * matching it to a declared expectation. + * Sub-classes must implement the actual validation of the request + * matching to declared expectations. */ - protected abstract ClientHttpResponse validateRequestInternal(ClientHttpRequest request) throws IOException; + protected abstract ClientHttpResponse validateRequestInternal(ClientHttpRequest request) + throws IOException; @Override public void verify() { - if (getExpectations().isEmpty()) { + if (this.expectations.isEmpty()) { return; } int count = 0; - for (RequestExpectation expectation : getExpectations()) { + for (RequestExpectation expectation : this.expectations) { if (!expectation.isSatisfied()) { count++; } @@ -114,10 +122,10 @@ public abstract class AbstractRequestExpectationManager implements RequestExpect */ protected String getRequestDetails() { StringBuilder sb = new StringBuilder(); - sb.append(getRequests().size()).append(" request(s) executed"); - if (!getRequests().isEmpty()) { + sb.append(this.requests.size()).append(" request(s) executed"); + if (!this.requests.isEmpty()) { sb.append(":\n"); - for (ClientHttpRequest request : getRequests()) { + for (ClientHttpRequest request : this.requests) { sb.append(request.toString()).append("\n"); } } @@ -146,9 +154,7 @@ public abstract class AbstractRequestExpectationManager implements RequestExpect /** - * Helper class to manage a group of request expectations. It helps with - * operations against the entire group such as finding a match and updating - * (add or remove) based on expected request count. + * Helper class to manage a group of remaining expectations. */ protected static class RequestExpectationGroup { @@ -158,24 +164,10 @@ public abstract class AbstractRequestExpectationManager implements RequestExpect return this.expectations; } - public void update(RequestExpectation expectation) { - if (expectation.hasRemainingCount()) { - getExpectations().add(expectation); - } - else { - getExpectations().remove(expectation); - } - } - - public void updateAll(Collection expectations) { - for (RequestExpectation expectation : expectations) { - update(expectation); - } - } @Nullable public RequestExpectation findExpectation(ClientHttpRequest request) throws IOException { - for (RequestExpectation expectation : getExpectations()) { + for (RequestExpectation expectation : this.expectations) { try { expectation.match(request); return expectation; @@ -187,8 +179,30 @@ public abstract class AbstractRequestExpectationManager implements RequestExpect return null; } + /** + * Invoke this for an expectation that has been matched. + *

The given expectation will either be stored if it has a remaining + * count or it will be removed otherwise. + */ + public void update(RequestExpectation expectation) { + if (expectation.hasRemainingCount()) { + this.expectations.add(expectation); + } + else { + this.expectations.remove(expectation); + } + } + + /** + * Collection variant of {@link #update(RequestExpectation)} that can + * be used to insert expectations. + */ + public void updateAll(Collection expectations) { + expectations.forEach(this::update); + } + public void reset() { - getExpectations().clear(); + this.expectations.clear(); } } 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 cb93e67faa..fdc4550a1e 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 @@ -273,7 +273,8 @@ public class MockRestServiceServer { * Mock ClientHttpRequestFactory that creates requests by iterating * over the list of expected {@link DefaultRequestExpectation}'s. */ - private class MockClientHttpRequestFactory implements ClientHttpRequestFactory, org.springframework.http.client.AsyncClientHttpRequestFactory { + private class MockClientHttpRequestFactory implements ClientHttpRequestFactory, + org.springframework.http.client.AsyncClientHttpRequestFactory { @Override public ClientHttpRequest createRequest(URI uri, HttpMethod httpMethod) { diff --git a/spring-test/src/main/java/org/springframework/test/web/client/UnorderedRequestExpectationManager.java b/spring-test/src/main/java/org/springframework/test/web/client/UnorderedRequestExpectationManager.java index 42c1a6aaac..8ddf20dfe7 100644 --- a/spring-test/src/main/java/org/springframework/test/web/client/UnorderedRequestExpectationManager.java +++ b/spring-test/src/main/java/org/springframework/test/web/client/UnorderedRequestExpectationManager.java @@ -41,12 +41,12 @@ public class UnorderedRequestExpectationManager extends AbstractRequestExpectati @Override public ClientHttpResponse validateRequestInternal(ClientHttpRequest request) throws IOException { RequestExpectation expectation = this.remainingExpectations.findExpectation(request); - if (expectation != null) { - ClientHttpResponse response = expectation.createResponse(request); - this.remainingExpectations.update(expectation); - return response; + if (expectation == null) { + throw createUnexpectedRequestError(request); } - throw createUnexpectedRequestError(request); + ClientHttpResponse response = expectation.createResponse(request); + this.remainingExpectations.update(expectation); + return response; } @Override diff --git a/spring-test/src/test/java/org/springframework/test/web/client/DefaultRequestExpectationTests.java b/spring-test/src/test/java/org/springframework/test/web/client/DefaultRequestExpectationTests.java index 3c27cadbf6..5c912b611c 100644 --- a/spring-test/src/test/java/org/springframework/test/web/client/DefaultRequestExpectationTests.java +++ b/spring-test/src/test/java/org/springframework/test/web/client/DefaultRequestExpectationTests.java @@ -28,14 +28,16 @@ import org.springframework.http.client.ClientHttpRequest; import static junit.framework.TestCase.assertFalse; import static org.junit.Assert.assertTrue; -import static org.springframework.http.HttpMethod.*; -import static org.springframework.test.web.client.ExpectedCount.*; -import static org.springframework.test.web.client.match.MockRestRequestMatchers.*; -import static org.springframework.test.web.client.response.MockRestResponseCreators.*; +import static org.springframework.http.HttpMethod.GET; +import static org.springframework.http.HttpMethod.POST; +import static org.springframework.test.web.client.ExpectedCount.once; +import static org.springframework.test.web.client.ExpectedCount.twice; +import static org.springframework.test.web.client.match.MockRestRequestMatchers.method; +import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo; +import static org.springframework.test.web.client.response.MockRestResponseCreators.withSuccess; /** * Unit tests for {@link DefaultRequestExpectation}. - * * @author Rossen Stoyanchev */ public class DefaultRequestExpectationTests { diff --git a/spring-test/src/test/java/org/springframework/test/web/client/MockRestServiceServerTests.java b/spring-test/src/test/java/org/springframework/test/web/client/MockRestServiceServerTests.java index 01ec0bac50..cc1297c46d 100644 --- a/spring-test/src/test/java/org/springframework/test/web/client/MockRestServiceServerTests.java +++ b/spring-test/src/test/java/org/springframework/test/web/client/MockRestServiceServerTests.java @@ -21,12 +21,11 @@ import org.junit.Test; import org.springframework.test.web.client.MockRestServiceServer.MockRestServiceServerBuilder; import org.springframework.web.client.RestTemplate; -import static org.springframework.test.web.client.match.MockRestRequestMatchers.*; -import static org.springframework.test.web.client.response.MockRestResponseCreators.*; +import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo; +import static org.springframework.test.web.client.response.MockRestResponseCreators.withSuccess; /** * Unit tests for {@link MockRestServiceServer}. - * * @author Rossen Stoyanchev */ public class MockRestServiceServerTests { diff --git a/spring-test/src/test/java/org/springframework/test/web/client/SimpleRequestExpectationManagerTests.java b/spring-test/src/test/java/org/springframework/test/web/client/SimpleRequestExpectationManagerTests.java index 4ecc56069d..15b828e416 100644 --- a/spring-test/src/test/java/org/springframework/test/web/client/SimpleRequestExpectationManagerTests.java +++ b/spring-test/src/test/java/org/springframework/test/web/client/SimpleRequestExpectationManagerTests.java @@ -27,15 +27,20 @@ import org.springframework.http.HttpMethod; import org.springframework.http.client.ClientHttpRequest; import org.springframework.mock.http.client.MockClientHttpRequest; -import static org.junit.Assert.*; -import static org.springframework.http.HttpMethod.*; -import static org.springframework.test.web.client.ExpectedCount.*; -import static org.springframework.test.web.client.match.MockRestRequestMatchers.*; -import static org.springframework.test.web.client.response.MockRestResponseCreators.*; +import static org.junit.Assert.assertEquals; +import static org.springframework.http.HttpMethod.GET; +import static org.springframework.http.HttpMethod.POST; +import static org.springframework.test.web.client.ExpectedCount.max; +import static org.springframework.test.web.client.ExpectedCount.min; +import static org.springframework.test.web.client.ExpectedCount.once; +import static org.springframework.test.web.client.ExpectedCount.times; +import static org.springframework.test.web.client.ExpectedCount.twice; +import static org.springframework.test.web.client.match.MockRestRequestMatchers.method; +import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo; +import static org.springframework.test.web.client.response.MockRestResponseCreators.withSuccess; /** * Unit tests for {@link SimpleRequestExpectationManager}. - * * @author Rossen Stoyanchev */ public class SimpleRequestExpectationManagerTests {