Browse Source

Improve exception message

Issue: SPR-12230
(cherry picked from commit 7f4bf41)
pull/690/head
Rossen Stoyanchev 11 years ago committed by Juergen Hoeller
parent
commit
2d0a677117
  1. 23
      spring-test/src/main/java/org/springframework/test/web/client/MockRestServiceServer.java
  2. 18
      spring-test/src/test/java/org/springframework/test/web/client/MockClientHttpRequestFactoryTests.java

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

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2012 the original author or authors. * Copyright 2002-2014 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.test.web.client; package org.springframework.test.web.client;
import java.io.IOException; import java.io.IOException;
@ -95,28 +96,24 @@ public class MockRestServiceServer {
private MockRestServiceServer() { private MockRestServiceServer() {
} }
/** /**
* Create a {@code MockRestServiceServer} and set up the given * Create a {@code MockRestServiceServer} and set up the given
* {@code RestTemplate} with a mock {@link ClientHttpRequestFactory}. * {@code RestTemplate} with a mock {@link ClientHttpRequestFactory}.
*
* @param restTemplate the RestTemplate to set up for mock testing * @param restTemplate the RestTemplate to set up for mock testing
* @return the created mock server * @return the created mock server
*/ */
public static MockRestServiceServer createServer(RestTemplate restTemplate) { public static MockRestServiceServer createServer(RestTemplate restTemplate) {
Assert.notNull(restTemplate, "'restTemplate' must not be null"); Assert.notNull(restTemplate, "'restTemplate' must not be null");
MockRestServiceServer mockServer = new MockRestServiceServer(); MockRestServiceServer mockServer = new MockRestServiceServer();
RequestMatcherClientHttpRequestFactory factory = mockServer.new RequestMatcherClientHttpRequestFactory(); RequestMatcherClientHttpRequestFactory factory = mockServer.new RequestMatcherClientHttpRequestFactory();
restTemplate.setRequestFactory(factory); restTemplate.setRequestFactory(factory);
return mockServer; return mockServer;
} }
/** /**
* Create a {@code MockRestServiceServer} and set up the given * Create a {@code MockRestServiceServer} and set up the given
* {@code RestGatewaySupport} with a mock {@link ClientHttpRequestFactory}. * {@code RestGatewaySupport} with a mock {@link ClientHttpRequestFactory}.
*
* @param restGateway the REST gateway to set up for mock testing * @param restGateway the REST gateway to set up for mock testing
* @return the created mock server * @return the created mock server
*/ */
@ -125,14 +122,12 @@ public class MockRestServiceServer {
return createServer(restGateway.getRestTemplate()); return createServer(restGateway.getRestTemplate());
} }
/** /**
* Set up a new HTTP request expectation. The returned {@link ResponseActions} * Set up a new HTTP request expectation. The returned {@link ResponseActions}
* is used to set up further expectations and to define the response. * is used to set up further expectations and to define the response.
* * <p>This method may be invoked multiple times before starting the test, i.e. before
* <p>This method may be invoked multiple times before starting the test, i.e. * using the {@code RestTemplate}, to set up expectations for multiple requests.
* before using the {@code RestTemplate}, to set up expectations for multiple
* requests.
*
* @param requestMatcher a request expectation, see {@link MockRestRequestMatchers} * @param requestMatcher a request expectation, see {@link MockRestRequestMatchers}
* @return used to set up further expectations or to define a response * @return used to set up further expectations or to define a response
*/ */
@ -146,7 +141,6 @@ public class MockRestServiceServer {
/** /**
* Verify that all expected requests set up via * Verify that all expected requests set up via
* {@link #expect(RequestMatcher)} were indeed performed. * {@link #expect(RequestMatcher)} were indeed performed.
*
* @throws AssertionError when some expectations were not met * @throws AssertionError when some expectations were not met
*/ */
public void verify() { public void verify() {
@ -158,7 +152,6 @@ public class MockRestServiceServer {
private String getVerifyMessage() { private String getVerifyMessage() {
StringBuilder sb = new StringBuilder("Further request(s) expected\n"); StringBuilder sb = new StringBuilder("Further request(s) expected\n");
if (this.actualRequests.size() > 0) { if (this.actualRequests.size() > 0) {
sb.append("The following "); sb.append("The following ");
} }
@ -171,7 +164,6 @@ public class MockRestServiceServer {
sb.append(request.toString()).append("\n"); sb.append(request.toString()).append("\n");
} }
} }
return sb.toString(); return sb.toString();
} }
@ -193,7 +185,7 @@ public class MockRestServiceServer {
this.requestIterator = MockRestServiceServer.this.expectedRequests.iterator(); this.requestIterator = MockRestServiceServer.this.expectedRequests.iterator();
} }
if (!this.requestIterator.hasNext()) { if (!this.requestIterator.hasNext()) {
throw new AssertionError("No further requests expected"); throw new AssertionError("No further requests expected: HTTP " + httpMethod + " " + uri);
} }
RequestMatcherClientHttpRequest request = this.requestIterator.next(); RequestMatcherClientHttpRequest request = this.requestIterator.next();
@ -201,7 +193,6 @@ public class MockRestServiceServer {
request.setMethod(httpMethod); request.setMethod(httpMethod);
MockRestServiceServer.this.actualRequests.add(request); MockRestServiceServer.this.actualRequests.add(request);
return request; return request;
} }
} }

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

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2012 the original author or authors. * Copyright 2002-2014 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -13,25 +13,25 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.test.web.client;
import static org.junit.Assert.assertEquals; package org.springframework.test.web.client;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.springframework.test.web.client.match.MockRestRequestMatchers.anything;
import java.net.URI; import java.net.URI;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.springframework.http.HttpMethod; import org.springframework.http.HttpMethod;
import org.springframework.http.client.ClientHttpRequest; import org.springframework.http.client.ClientHttpRequest;
import org.springframework.http.client.ClientHttpRequestFactory; import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.test.web.client.MockRestServiceServer;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
import static org.junit.Assert.*;
import static org.springframework.test.web.client.match.MockRestRequestMatchers.*;
/** /**
* Tests for {@link MockClientHttpRequestFactory}. * Tests for
* {@link org.springframework.test.web.client.MockMvcClientHttpRequestFactory}.
* *
* @author Rossen Stoyanchev * @author Rossen Stoyanchev
*/ */
@ -66,7 +66,7 @@ public class MockClientHttpRequestFactoryTests {
this.factory.createRequest(new URI("/foo"), HttpMethod.GET); this.factory.createRequest(new URI("/foo"), HttpMethod.GET);
} }
catch (AssertionError error) { catch (AssertionError error) {
assertEquals("No further requests expected", error.getMessage()); assertEquals("No further requests expected: HTTP GET /foo", error.getMessage());
} }
} }

Loading…
Cancel
Save