Skip to content

Commit 6b353e8

Browse files
rstoyanchevjhoeller
authored andcommitted
Improve exception message
Issue: SPR-12230 (cherry picked from commit 2d0a677)
1 parent 983ebaf commit 6b353e8

File tree

2 files changed

+17
-26
lines changed

2 files changed

+17
-26
lines changed

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

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2014 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.
@@ -13,6 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16+
1617
package org.springframework.test.web.client;
1718

1819
import java.io.IOException;
@@ -39,7 +40,7 @@
3940
* actual running server.
4041
*
4142
* <p>Below is an example:
42-
* <pre>
43+
* <pre class="code">
4344
* RestTemplate restTemplate = new RestTemplate()
4445
* MockRestServiceServer mockServer = MockRestServiceServer.createServer(restTemplate);
4546
*
@@ -95,28 +96,24 @@ public class MockRestServiceServer {
9596
private MockRestServiceServer() {
9697
}
9798

99+
98100
/**
99101
* Create a {@code MockRestServiceServer} and set up the given
100102
* {@code RestTemplate} with a mock {@link ClientHttpRequestFactory}.
101-
*
102103
* @param restTemplate the RestTemplate to set up for mock testing
103104
* @return the created mock server
104105
*/
105106
public static MockRestServiceServer createServer(RestTemplate restTemplate) {
106107
Assert.notNull(restTemplate, "'restTemplate' must not be null");
107-
108108
MockRestServiceServer mockServer = new MockRestServiceServer();
109109
RequestMatcherClientHttpRequestFactory factory = mockServer.new RequestMatcherClientHttpRequestFactory();
110-
111110
restTemplate.setRequestFactory(factory);
112-
113111
return mockServer;
114112
}
115113

116114
/**
117115
* Create a {@code MockRestServiceServer} and set up the given
118116
* {@code RestGatewaySupport} with a mock {@link ClientHttpRequestFactory}.
119-
*
120117
* @param restGateway the REST gateway to set up for mock testing
121118
* @return the created mock server
122119
*/
@@ -125,14 +122,12 @@ public static MockRestServiceServer createServer(RestGatewaySupport restGateway)
125122
return createServer(restGateway.getRestTemplate());
126123
}
127124

125+
128126
/**
129127
* Set up a new HTTP request expectation. The returned {@link ResponseActions}
130128
* 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.
136131
* @param requestMatcher a request expectation, see {@link MockRestRequestMatchers}
137132
* @return used to set up further expectations or to define a response
138133
*/
@@ -146,7 +141,6 @@ public ResponseActions expect(RequestMatcher requestMatcher) {
146141
/**
147142
* Verify that all expected requests set up via
148143
* {@link #expect(RequestMatcher)} were indeed performed.
149-
*
150144
* @throws AssertionError when some expectations were not met
151145
*/
152146
public void verify() {
@@ -158,7 +152,6 @@ public void verify() {
158152

159153
private String getVerifyMessage() {
160154
StringBuilder sb = new StringBuilder("Further request(s) expected\n");
161-
162155
if (this.actualRequests.size() > 0) {
163156
sb.append("The following ");
164157
}
@@ -171,7 +164,6 @@ private String getVerifyMessage() {
171164
sb.append(request.toString()).append("\n");
172165
}
173166
}
174-
175167
return sb.toString();
176168
}
177169

@@ -192,15 +184,14 @@ public ClientHttpRequest createRequest(URI uri, HttpMethod httpMethod) throws IO
192184
this.requestIterator = MockRestServiceServer.this.expectedRequests.iterator();
193185
}
194186
if (!this.requestIterator.hasNext()) {
195-
throw new AssertionError("No further requests expected");
187+
throw new AssertionError("No further requests expected: HTTP " + httpMethod + " " + uri);
196188
}
197189

198190
RequestMatcherClientHttpRequest request = this.requestIterator.next();
199191
request.setURI(uri);
200192
request.setMethod(httpMethod);
201193

202194
MockRestServiceServer.this.actualRequests.add(request);
203-
204195
return request;
205196
}
206197
}

spring-test-mvc/src/test/java/org/springframework/test/web/client/MockClientHttpRequestFactoryTests.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2014 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.
@@ -13,25 +13,25 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package org.springframework.test.web.client;
1716

18-
import static org.junit.Assert.assertEquals;
19-
import static org.junit.Assert.assertSame;
20-
import static org.junit.Assert.assertTrue;
21-
import static org.springframework.test.web.client.match.MockRestRequestMatchers.anything;
17+
package org.springframework.test.web.client;
2218

2319
import java.net.URI;
2420

2521
import org.junit.Before;
2622
import org.junit.Test;
23+
2724
import org.springframework.http.HttpMethod;
2825
import org.springframework.http.client.ClientHttpRequest;
2926
import org.springframework.http.client.ClientHttpRequestFactory;
30-
import org.springframework.test.web.client.MockRestServiceServer;
3127
import org.springframework.web.client.RestTemplate;
3228

29+
import static org.junit.Assert.*;
30+
import static org.springframework.test.web.client.match.MockRestRequestMatchers.*;
31+
3332
/**
34-
* Tests for {@link MockClientHttpRequestFactory}.
33+
* Tests for
34+
* {@link org.springframework.test.web.client.MockMvcClientHttpRequestFactory}.
3535
*
3636
* @author Rossen Stoyanchev
3737
*/
@@ -66,7 +66,7 @@ public void noFurtherRequestsExpected() throws Exception {
6666
this.factory.createRequest(new URI("/foo"), HttpMethod.GET);
6767
}
6868
catch (AssertionError error) {
69-
assertEquals("No further requests expected", error.getMessage());
69+
assertEquals("No further requests expected: HTTP GET /foo", error.getMessage());
7070
}
7171
}
7272

0 commit comments

Comments
 (0)