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 9f16129267..e2c5d848a0 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 @@ -75,16 +75,15 @@ public abstract class AbstractRequestExpectationManager implements RequestExpect @Override public ClientHttpResponse validateRequest(ClientHttpRequest request) throws IOException { - List requests = this.requests; - synchronized (requests) { - if (requests.isEmpty()) { + synchronized (this.requests) { + if (this.requests.isEmpty()) { afterExpectationsDeclared(); } try { return validateRequestInternal(request); } finally { - requests.add(request); + this.requests.add(request); } } } @@ -97,7 +96,7 @@ public abstract class AbstractRequestExpectationManager implements RequestExpect } /** - * Sub-classes must implement the actual validation of the request + * Subclasses must implement the actual validation of the request * matching to declared expectations. */ protected abstract ClientHttpResponse validateRequestInternal(ClientHttpRequest request) 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 cc1297c46d..49c5c1b5f2 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 @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,25 +16,29 @@ package org.springframework.test.web.client; +import java.net.SocketException; + 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.requestTo; -import static org.springframework.test.web.client.response.MockRestResponseCreators.withSuccess; +import static org.springframework.http.HttpMethod.*; +import static org.springframework.test.web.client.match.MockRestRequestMatchers.*; +import static org.springframework.test.web.client.response.MockRestResponseCreators.*; /** * Unit tests for {@link MockRestServiceServer}. + * * @author Rossen Stoyanchev */ public class MockRestServiceServerTests { - private RestTemplate restTemplate = new RestTemplate(); + private final RestTemplate restTemplate = new RestTemplate(); @Test - public void buildMultipleTimes() throws Exception { + public void buildMultipleTimes() { MockRestServiceServerBuilder builder = MockRestServiceServer.bindTo(this.restTemplate); MockRestServiceServer server = builder.build(); @@ -56,7 +60,7 @@ public class MockRestServiceServerTests { } @Test(expected = AssertionError.class) - public void exactExpectOrder() throws Exception { + public void exactExpectOrder() { MockRestServiceServer server = MockRestServiceServer.bindTo(this.restTemplate) .ignoreExpectOrder(false).build(); @@ -66,7 +70,7 @@ public class MockRestServiceServerTests { } @Test - public void ignoreExpectOrder() throws Exception { + public void ignoreExpectOrder() { MockRestServiceServer server = MockRestServiceServer.bindTo(this.restTemplate) .ignoreExpectOrder(true).build(); @@ -78,7 +82,7 @@ public class MockRestServiceServerTests { } @Test - public void resetAndReuseServer() throws Exception { + public void resetAndReuseServer() { MockRestServiceServer server = MockRestServiceServer.bindTo(this.restTemplate).build(); server.expect(requestTo("/foo")).andRespond(withSuccess()); @@ -92,7 +96,7 @@ public class MockRestServiceServerTests { } @Test - public void resetAndReuseServerWithUnorderedExpectationManager() throws Exception { + public void resetAndReuseServerWithUnorderedExpectationManager() { MockRestServiceServer server = MockRestServiceServer.bindTo(this.restTemplate) .ignoreExpectOrder(true).build(); @@ -108,4 +112,24 @@ public class MockRestServiceServerTests { server.verify(); } + @Test // SPR-16132 + public void followUpRequestAfterFailure() { + MockRestServiceServer server = MockRestServiceServer.bindTo(this.restTemplate).build(); + + server.expect(requestTo("/some-service/some-endpoint")) + .andRespond(request -> { throw new SocketException("pseudo network error"); }); + + server.expect(requestTo("/reporting-service/report-error")) + .andExpect(method(POST)).andRespond(withSuccess()); + + try { + this.restTemplate.getForEntity("/some-service/some-endpoint", String.class); + } + catch (Exception ex) { + this.restTemplate.postForEntity("/reporting-service/report-error", ex.toString(), String.class); + } + + server.verify(); + } + } 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 68a0925503..732726509f 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 @@ -28,21 +28,15 @@ import org.springframework.http.HttpMethod; import org.springframework.http.client.ClientHttpRequest; import org.springframework.mock.http.client.MockClientHttpRequest; -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.util.AssertionErrors.fail; -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; +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.*; /** * Unit tests for {@link SimpleRequestExpectationManager}. + * * @author Rossen Stoyanchev */ public class SimpleRequestExpectationManagerTests { @@ -188,16 +182,17 @@ public class SimpleRequestExpectationManagerTests { @Test // SPR-16132 public void sequentialRequestsWithFirstFailing() throws Exception { - this.manager.expectRequest(once(), requestTo("/foo")).andExpect(method(GET)) - .andRespond(request -> { throw new SocketException("pseudo network error"); }); - this.manager.expectRequest(once(), requestTo("/handle-error")).andExpect(method(POST)).andRespond(withSuccess()); + this.manager.expectRequest(once(), requestTo("/foo")). + andExpect(method(GET)).andRespond(request -> { throw new SocketException("pseudo network error"); }); + this.manager.expectRequest(once(), requestTo("/handle-error")). + andExpect(method(POST)).andRespond(withSuccess()); try { this.manager.validateRequest(createRequest(GET, "/foo")); - fail("expected exception"); + fail("Expected SocketException"); } - catch (SocketException e) { - //expected + catch (SocketException ex) { + // expected } this.manager.validateRequest(createRequest(POST, "/handle-error")); this.manager.verify(); diff --git a/spring-test/src/test/java/org/springframework/test/web/client/UnorderedRequestExpectationManagerTests.java b/spring-test/src/test/java/org/springframework/test/web/client/UnorderedRequestExpectationManagerTests.java index 9b4b8d0a05..bcf8777cd7 100644 --- a/spring-test/src/test/java/org/springframework/test/web/client/UnorderedRequestExpectationManagerTests.java +++ b/spring-test/src/test/java/org/springframework/test/web/client/UnorderedRequestExpectationManagerTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -26,18 +26,15 @@ import org.junit.rules.ExpectedException; import org.springframework.http.HttpMethod; import org.springframework.http.client.ClientHttpRequest; -import static org.junit.Assert.assertEquals; -import static org.springframework.http.HttpMethod.GET; -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.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; +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.*; /** * Unit tests for {@link UnorderedRequestExpectationManager}. + * * @author Rossen Stoyanchev */ public class UnorderedRequestExpectationManagerTests { @@ -131,4 +128,5 @@ public class UnorderedRequestExpectationManagerTests { throw new IllegalStateException(ex); } } + }