1
1
/*
2
- * Copyright 2002-2012 the original author or authors.
2
+ * Copyright 2002-2014 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
13
13
* See the License for the specific language governing permissions and
14
14
* limitations under the License.
15
15
*/
16
+
16
17
package org .springframework .test .web .client ;
17
18
18
19
import java .io .IOException ;
39
40
* actual running server.
40
41
*
41
42
* <p>Below is an example:
42
- * <pre>
43
+ * <pre class="code" >
43
44
* RestTemplate restTemplate = new RestTemplate()
44
45
* MockRestServiceServer mockServer = MockRestServiceServer.createServer(restTemplate);
45
46
*
@@ -95,28 +96,24 @@ public class MockRestServiceServer {
95
96
private MockRestServiceServer () {
96
97
}
97
98
99
+
98
100
/**
99
101
* Create a {@code MockRestServiceServer} and set up the given
100
102
* {@code RestTemplate} with a mock {@link ClientHttpRequestFactory}.
101
- *
102
103
* @param restTemplate the RestTemplate to set up for mock testing
103
104
* @return the created mock server
104
105
*/
105
106
public static MockRestServiceServer createServer (RestTemplate restTemplate ) {
106
107
Assert .notNull (restTemplate , "'restTemplate' must not be null" );
107
-
108
108
MockRestServiceServer mockServer = new MockRestServiceServer ();
109
109
RequestMatcherClientHttpRequestFactory factory = mockServer .new RequestMatcherClientHttpRequestFactory ();
110
-
111
110
restTemplate .setRequestFactory (factory );
112
-
113
111
return mockServer ;
114
112
}
115
113
116
114
/**
117
115
* Create a {@code MockRestServiceServer} and set up the given
118
116
* {@code RestGatewaySupport} with a mock {@link ClientHttpRequestFactory}.
119
- *
120
117
* @param restGateway the REST gateway to set up for mock testing
121
118
* @return the created mock server
122
119
*/
@@ -125,14 +122,12 @@ public static MockRestServiceServer createServer(RestGatewaySupport restGateway)
125
122
return createServer (restGateway .getRestTemplate ());
126
123
}
127
124
125
+
128
126
/**
129
127
* Set up a new HTTP request expectation. The returned {@link ResponseActions}
130
128
* is used to set up further expectations and to define the response.
131
- *
132
- * <p>This method may be invoked multiple times before starting the test, i.e.
133
- * before using the {@code RestTemplate}, to set up expectations for multiple
134
- * requests.
135
- *
129
+ * <p>This method may be invoked multiple times before starting the test, i.e. before
130
+ * using the {@code RestTemplate}, to set up expectations for multiple requests.
136
131
* @param requestMatcher a request expectation, see {@link MockRestRequestMatchers}
137
132
* @return used to set up further expectations or to define a response
138
133
*/
@@ -146,7 +141,6 @@ public ResponseActions expect(RequestMatcher requestMatcher) {
146
141
/**
147
142
* Verify that all expected requests set up via
148
143
* {@link #expect(RequestMatcher)} were indeed performed.
149
- *
150
144
* @throws AssertionError when some expectations were not met
151
145
*/
152
146
public void verify () {
@@ -158,7 +152,6 @@ public void verify() {
158
152
159
153
private String getVerifyMessage () {
160
154
StringBuilder sb = new StringBuilder ("Further request(s) expected\n " );
161
-
162
155
if (this .actualRequests .size () > 0 ) {
163
156
sb .append ("The following " );
164
157
}
@@ -171,7 +164,6 @@ private String getVerifyMessage() {
171
164
sb .append (request .toString ()).append ("\n " );
172
165
}
173
166
}
174
-
175
167
return sb .toString ();
176
168
}
177
169
@@ -192,15 +184,14 @@ public ClientHttpRequest createRequest(URI uri, HttpMethod httpMethod) throws IO
192
184
this .requestIterator = MockRestServiceServer .this .expectedRequests .iterator ();
193
185
}
194
186
if (!this .requestIterator .hasNext ()) {
195
- throw new AssertionError ("No further requests expected" );
187
+ throw new AssertionError ("No further requests expected: HTTP " + httpMethod + " " + uri );
196
188
}
197
189
198
190
RequestMatcherClientHttpRequest request = this .requestIterator .next ();
199
191
request .setURI (uri );
200
192
request .setMethod (httpMethod );
201
193
202
194
MockRestServiceServer .this .actualRequests .add (request );
203
-
204
195
return request ;
205
196
}
206
197
}
0 commit comments