Skip to content

Commit d62dd2d

Browse files
committed
Polish MockRestServiceServer code
Cherry picked from #7ab4d0ca08
1 parent e935018 commit d62dd2d

File tree

4 files changed

+37
-16
lines changed

4 files changed

+37
-16
lines changed

spring-test/src/main/java/org/springframework/test/web/client/AbstractRequestExpectationManager.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,14 +159,17 @@ public Set<RequestExpectation> getExpectations() {
159159
return this.expectations;
160160
}
161161

162+
/**
163+
* Return a matching expectation, or {@code null} if none match.
164+
*/
162165
public RequestExpectation findExpectation(ClientHttpRequest request) throws IOException {
163166
for (RequestExpectation expectation : getExpectations()) {
164167
try {
165168
expectation.match(request);
166169
return expectation;
167170
}
168171
catch (AssertionError error) {
169-
// Ignore
172+
// We're looking to find a match or return null..
170173
}
171174
}
172175
return null;

spring-test/src/main/java/org/springframework/test/web/client/MockRestServiceServer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public ResponseActions expect(RequestMatcher matcher) {
9494
}
9595

9696
/**
97-
* An alternative to {@link #expect(RequestMatcher)} with an indication how
97+
* An alternative to {@link #expect(RequestMatcher)} that also indicates how
9898
* many times the request is expected to be executed.
9999
* <p>When request expectations have an expected count greater than one, only
100100
* the first execution is expected to match the order of declaration. Subsequent

spring-test/src/main/java/org/springframework/test/web/client/RequestExpectationManager.java

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -22,9 +22,14 @@
2222
import org.springframework.http.client.ClientHttpResponse;
2323

2424
/**
25-
* Abstraction for creating HTTP request expectations, applying them to actual
26-
* requests (in strict or random order), and verifying whether expectations
27-
* have been met.
25+
* Encapsulates the behavior required to implement {@link MockRestServiceServer}
26+
* including its public API (create expectations + verify/reset) along with an
27+
* extra method for verifying actual requests.
28+
*
29+
* <p>This contract is not used directly in applications but a custom
30+
* implementation can be
31+
* {@link org.springframework.test.web.client.MockRestServiceServer.MockRestServiceServerBuilder#build(RequestExpectationManager)
32+
* plugged} in through the {@code MockRestServiceServer} builder.
2833
*
2934
* @author Rossen Stoyanchev
3035
* @since 4.3
@@ -34,30 +39,41 @@ public interface RequestExpectationManager {
3439
/**
3540
* Set up a new request expectation. The returned {@link ResponseActions} is
3641
* used to add more expectations and define a response.
42+
* <p>This is a delegate for
43+
* {@link MockRestServiceServer#expect(ExpectedCount, RequestMatcher)}.
44+
*
3745
* @param requestMatcher a request expectation
3846
* @return for setting up further expectations and define a response
47+
* @see MockRestServiceServer#expect(RequestMatcher)
48+
* @see MockRestServiceServer#expect(ExpectedCount, RequestMatcher)
3949
*/
4050
ResponseActions expectRequest(ExpectedCount count, RequestMatcher requestMatcher);
4151

42-
/**
43-
* Validate the given actual request against the declared expectations.
44-
* Is successful return the mock response to use or raise an error.
45-
* @param request the request
46-
* @return the response to return if the request was validated.
47-
* @throws AssertionError when some expectations were not met
48-
* @throws IOException in case of any validation errors
49-
*/
50-
ClientHttpResponse validateRequest(ClientHttpRequest request) throws IOException;
51-
5252
/**
5353
* Verify that all expectations have been met.
54+
* <p>This is a delegate for {@link MockRestServiceServer#verify()}.
5455
* @throws AssertionError when some expectations were not met
56+
* @see MockRestServiceServer#verify()
5557
*/
5658
void verify();
5759

5860
/**
5961
* Reset the internal state removing all expectations and recorded requests.
62+
* <p>This is a delegate for {@link MockRestServiceServer#reset()}.
63+
* @see MockRestServiceServer#reset()
6064
*/
6165
void reset();
6266

67+
68+
/**
69+
* Validate the given actual request against the declared expectations.
70+
* Is successful return the mock response to use or raise an error.
71+
* <p>This is used in {@link MockRestServiceServer} against actual requests.
72+
* @param request the request
73+
* @return the response to return if the request was validated.
74+
* @throws AssertionError when some expectations were not met
75+
* @throws IOException in case of any validation errors
76+
*/
77+
ClientHttpResponse validateRequest(ClientHttpRequest request) throws IOException;
78+
6379
}

spring-test/src/main/java/org/springframework/test/web/client/SimpleRequestExpectationManager.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,10 @@
3737
*/
3838
public class SimpleRequestExpectationManager extends AbstractRequestExpectationManager {
3939

40+
/** Expectations in the order of declaration (count may be > 1) */
4041
private Iterator<RequestExpectation> expectationIterator;
4142

43+
/** Track expectations that have a remaining count */
4244
private final RequestExpectationGroup repeatExpectations = new RequestExpectationGroup();
4345

4446

0 commit comments

Comments
 (0)