Skip to content

Commit 295e3b6

Browse files
committed
MockRestServiceServer test for follow-up request after failure
Issue: SPR-16132
1 parent 3c07afc commit 295e3b6

File tree

4 files changed

+58
-42
lines changed

4 files changed

+58
-42
lines changed

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,16 +75,15 @@ public ResponseActions expectRequest(ExpectedCount count, RequestMatcher matcher
7575

7676
@Override
7777
public ClientHttpResponse validateRequest(ClientHttpRequest request) throws IOException {
78-
List<ClientHttpRequest> requests = this.requests;
79-
synchronized (requests) {
80-
if (requests.isEmpty()) {
78+
synchronized (this.requests) {
79+
if (this.requests.isEmpty()) {
8180
afterExpectationsDeclared();
8281
}
8382
try {
8483
return validateRequestInternal(request);
8584
}
8685
finally {
87-
requests.add(request);
86+
this.requests.add(request);
8887
}
8988
}
9089
}
@@ -97,7 +96,7 @@ protected void afterExpectationsDeclared() {
9796
}
9897

9998
/**
100-
* Sub-classes must implement the actual validation of the request
99+
* Subclasses must implement the actual validation of the request
101100
* matching to declared expectations.
102101
*/
103102
protected abstract ClientHttpResponse validateRequestInternal(ClientHttpRequest request)

spring-test/src/test/java/org/springframework/test/web/client/MockRestServiceServerTests.java

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2017 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.
@@ -16,25 +16,29 @@
1616

1717
package org.springframework.test.web.client;
1818

19+
import java.net.SocketException;
20+
1921
import org.junit.Test;
2022

2123
import org.springframework.test.web.client.MockRestServiceServer.MockRestServiceServerBuilder;
2224
import org.springframework.web.client.RestTemplate;
2325

24-
import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo;
25-
import static org.springframework.test.web.client.response.MockRestResponseCreators.withSuccess;
26+
import static org.springframework.http.HttpMethod.*;
27+
import static org.springframework.test.web.client.match.MockRestRequestMatchers.*;
28+
import static org.springframework.test.web.client.response.MockRestResponseCreators.*;
2629

2730
/**
2831
* Unit tests for {@link MockRestServiceServer}.
32+
*
2933
* @author Rossen Stoyanchev
3034
*/
3135
public class MockRestServiceServerTests {
3236

33-
private RestTemplate restTemplate = new RestTemplate();
37+
private final RestTemplate restTemplate = new RestTemplate();
3438

3539

3640
@Test
37-
public void buildMultipleTimes() throws Exception {
41+
public void buildMultipleTimes() {
3842
MockRestServiceServerBuilder builder = MockRestServiceServer.bindTo(this.restTemplate);
3943

4044
MockRestServiceServer server = builder.build();
@@ -56,7 +60,7 @@ public void buildMultipleTimes() throws Exception {
5660
}
5761

5862
@Test(expected = AssertionError.class)
59-
public void exactExpectOrder() throws Exception {
63+
public void exactExpectOrder() {
6064
MockRestServiceServer server = MockRestServiceServer.bindTo(this.restTemplate)
6165
.ignoreExpectOrder(false).build();
6266

@@ -66,7 +70,7 @@ public void exactExpectOrder() throws Exception {
6670
}
6771

6872
@Test
69-
public void ignoreExpectOrder() throws Exception {
73+
public void ignoreExpectOrder() {
7074
MockRestServiceServer server = MockRestServiceServer.bindTo(this.restTemplate)
7175
.ignoreExpectOrder(true).build();
7276

@@ -78,7 +82,7 @@ public void ignoreExpectOrder() throws Exception {
7882
}
7983

8084
@Test
81-
public void resetAndReuseServer() throws Exception {
85+
public void resetAndReuseServer() {
8286
MockRestServiceServer server = MockRestServiceServer.bindTo(this.restTemplate).build();
8387

8488
server.expect(requestTo("/foo")).andRespond(withSuccess());
@@ -92,7 +96,7 @@ public void resetAndReuseServer() throws Exception {
9296
}
9397

9498
@Test
95-
public void resetAndReuseServerWithUnorderedExpectationManager() throws Exception {
99+
public void resetAndReuseServerWithUnorderedExpectationManager() {
96100
MockRestServiceServer server = MockRestServiceServer.bindTo(this.restTemplate)
97101
.ignoreExpectOrder(true).build();
98102

@@ -108,4 +112,24 @@ public void resetAndReuseServerWithUnorderedExpectationManager() throws Exceptio
108112
server.verify();
109113
}
110114

115+
@Test // SPR-16132
116+
public void followUpRequestAfterFailure() {
117+
MockRestServiceServer server = MockRestServiceServer.bindTo(this.restTemplate).build();
118+
119+
server.expect(requestTo("/some-service/some-endpoint"))
120+
.andRespond(request -> { throw new SocketException("pseudo network error"); });
121+
122+
server.expect(requestTo("/reporting-service/report-error"))
123+
.andExpect(method(POST)).andRespond(withSuccess());
124+
125+
try {
126+
this.restTemplate.getForEntity("/some-service/some-endpoint", String.class);
127+
}
128+
catch (Exception ex) {
129+
this.restTemplate.postForEntity("/reporting-service/report-error", ex.toString(), String.class);
130+
}
131+
132+
server.verify();
133+
}
134+
111135
}

spring-test/src/test/java/org/springframework/test/web/client/SimpleRequestExpectationManagerTests.java

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,15 @@
2828
import org.springframework.http.client.ClientHttpRequest;
2929
import org.springframework.mock.http.client.MockClientHttpRequest;
3030

31-
import static org.junit.Assert.assertEquals;
32-
import static org.springframework.http.HttpMethod.GET;
33-
import static org.springframework.http.HttpMethod.POST;
34-
import static org.springframework.test.util.AssertionErrors.fail;
35-
import static org.springframework.test.web.client.ExpectedCount.max;
36-
import static org.springframework.test.web.client.ExpectedCount.min;
37-
import static org.springframework.test.web.client.ExpectedCount.once;
38-
import static org.springframework.test.web.client.ExpectedCount.times;
39-
import static org.springframework.test.web.client.ExpectedCount.twice;
40-
import static org.springframework.test.web.client.match.MockRestRequestMatchers.method;
41-
import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo;
42-
import static org.springframework.test.web.client.response.MockRestResponseCreators.withSuccess;
31+
import static org.junit.Assert.*;
32+
import static org.springframework.http.HttpMethod.*;
33+
import static org.springframework.test.web.client.ExpectedCount.*;
34+
import static org.springframework.test.web.client.match.MockRestRequestMatchers.*;
35+
import static org.springframework.test.web.client.response.MockRestResponseCreators.*;
4336

4437
/**
4538
* Unit tests for {@link SimpleRequestExpectationManager}.
39+
*
4640
* @author Rossen Stoyanchev
4741
*/
4842
public class SimpleRequestExpectationManagerTests {
@@ -188,16 +182,17 @@ public void repeatedRequestsInSequentialOrder() throws Exception {
188182

189183
@Test // SPR-16132
190184
public void sequentialRequestsWithFirstFailing() throws Exception {
191-
this.manager.expectRequest(once(), requestTo("/foo")).andExpect(method(GET))
192-
.andRespond(request -> { throw new SocketException("pseudo network error"); });
193-
this.manager.expectRequest(once(), requestTo("/handle-error")).andExpect(method(POST)).andRespond(withSuccess());
185+
this.manager.expectRequest(once(), requestTo("/foo")).
186+
andExpect(method(GET)).andRespond(request -> { throw new SocketException("pseudo network error"); });
187+
this.manager.expectRequest(once(), requestTo("/handle-error")).
188+
andExpect(method(POST)).andRespond(withSuccess());
194189

195190
try {
196191
this.manager.validateRequest(createRequest(GET, "/foo"));
197-
fail("expected exception");
192+
fail("Expected SocketException");
198193
}
199-
catch (SocketException e) {
200-
//expected
194+
catch (SocketException ex) {
195+
// expected
201196
}
202197
this.manager.validateRequest(createRequest(POST, "/handle-error"));
203198
this.manager.verify();

spring-test/src/test/java/org/springframework/test/web/client/UnorderedRequestExpectationManagerTests.java

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2017 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.
@@ -26,18 +26,15 @@
2626
import org.springframework.http.HttpMethod;
2727
import org.springframework.http.client.ClientHttpRequest;
2828

29-
import static org.junit.Assert.assertEquals;
30-
import static org.springframework.http.HttpMethod.GET;
31-
import static org.springframework.test.web.client.ExpectedCount.max;
32-
import static org.springframework.test.web.client.ExpectedCount.min;
33-
import static org.springframework.test.web.client.ExpectedCount.once;
34-
import static org.springframework.test.web.client.ExpectedCount.twice;
35-
import static org.springframework.test.web.client.match.MockRestRequestMatchers.method;
36-
import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo;
37-
import static org.springframework.test.web.client.response.MockRestResponseCreators.withSuccess;
29+
import static org.junit.Assert.*;
30+
import static org.springframework.http.HttpMethod.*;
31+
import static org.springframework.test.web.client.ExpectedCount.*;
32+
import static org.springframework.test.web.client.match.MockRestRequestMatchers.*;
33+
import static org.springframework.test.web.client.response.MockRestResponseCreators.*;
3834

3935
/**
4036
* Unit tests for {@link UnorderedRequestExpectationManager}.
37+
*
4138
* @author Rossen Stoyanchev
4239
*/
4340
public class UnorderedRequestExpectationManagerTests {
@@ -131,4 +128,5 @@ private ClientHttpRequest createRequest(HttpMethod method, String url) {
131128
throw new IllegalStateException(ex);
132129
}
133130
}
131+
134132
}

0 commit comments

Comments
 (0)