Skip to content

Commit ed70978

Browse files
committed
verify() fails if there are failed requests
Normally failed requests fail the test but they're suppressed for some reason (e.g. in async callback) then verify should still correctly report the failures. Closes gh-21799
1 parent 4be605e commit ed70978

File tree

3 files changed

+48
-5
lines changed

3 files changed

+48
-5
lines changed

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

+18-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 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.
@@ -20,10 +20,13 @@
2020
import java.net.URI;
2121
import java.util.Collection;
2222
import java.util.Collections;
23+
import java.util.LinkedHashMap;
2324
import java.util.LinkedHashSet;
2425
import java.util.LinkedList;
2526
import java.util.List;
27+
import java.util.Map;
2628
import java.util.Set;
29+
import java.util.stream.Collectors;
2730

2831
import org.springframework.http.HttpMethod;
2932
import org.springframework.http.client.ClientHttpRequest;
@@ -49,6 +52,8 @@ public abstract class AbstractRequestExpectationManager implements RequestExpect
4952

5053
private final List<ClientHttpRequest> requests = new LinkedList<>();
5154

55+
private final Map<ClientHttpRequest, Throwable> requestFailures = new LinkedHashMap<>();
56+
5257

5358
/**
5459
* Return a read-only list of the expectations.
@@ -91,6 +96,10 @@ public ClientHttpResponse validateRequest(ClientHttpRequest request) throws IOEx
9196
expectation = matchRequest(request);
9297
}
9398
}
99+
catch (Throwable ex) {
100+
this.requestFailures.put(request, ex);
101+
throw ex;
102+
}
94103
finally {
95104
this.requests.add(request);
96105
}
@@ -129,7 +138,8 @@ protected ClientHttpResponse validateRequestInternal(ClientHttpRequest request)
129138
* @since 5.0.3
130139
*/
131140
protected RequestExpectation matchRequest(ClientHttpRequest request) throws IOException {
132-
throw new UnsupportedOperationException("It looks like neither the deprecated \"validateRequestInternal\"" +
141+
throw new UnsupportedOperationException(
142+
"It looks like neither the deprecated \"validateRequestInternal\"" +
133143
"nor its replacement (this method) are implemented.");
134144
}
135145

@@ -148,6 +158,12 @@ public void verify() {
148158
String message = "Further request(s) expected leaving " + count + " unsatisfied expectation(s).\n";
149159
throw new AssertionError(message + getRequestDetails());
150160
}
161+
if (!this.requestFailures.isEmpty()) {
162+
throw new AssertionError("Some requests did not execute successfully.\n" +
163+
this.requestFailures.entrySet().stream()
164+
.map(entry -> "Failed request:\n" + entry.getKey() + "\n" + entry.getValue())
165+
.collect(Collectors.joining("\n", "\n", "")));
166+
}
151167
}
152168

153169
/**

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

+6-2
Original file line numberDiff line numberDiff line change
@@ -305,11 +305,15 @@ public ClientHttpRequest createRequest(URI uri, HttpMethod httpMethod) {
305305
}
306306

307307
@Override
308-
public org.springframework.http.client.AsyncClientHttpRequest createAsyncRequest(URI uri, HttpMethod httpMethod) {
308+
public org.springframework.http.client.AsyncClientHttpRequest createAsyncRequest(
309+
URI uri, HttpMethod httpMethod) {
310+
309311
return createRequestInternal(uri, httpMethod);
310312
}
311313

312-
private org.springframework.mock.http.client.MockAsyncClientHttpRequest createRequestInternal(URI uri, HttpMethod method) {
314+
private org.springframework.mock.http.client.MockAsyncClientHttpRequest createRequestInternal(
315+
URI uri, HttpMethod method) {
316+
313317
Assert.notNull(uri, "'uri' must not be null");
314318
Assert.notNull(method, "'httpMethod' must not be null");
315319

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

+24-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2019 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.
@@ -23,7 +23,9 @@
2323
import org.springframework.test.web.client.MockRestServiceServer.MockRestServiceServerBuilder;
2424
import org.springframework.web.client.RestTemplate;
2525

26+
import static org.assertj.core.api.Assertions.*;
2627
import static org.springframework.http.HttpMethod.*;
28+
import static org.springframework.test.web.client.ExpectedCount.*;
2729
import static org.springframework.test.web.client.match.MockRestRequestMatchers.*;
2830
import static org.springframework.test.web.client.response.MockRestResponseCreators.*;
2931

@@ -132,4 +134,25 @@ public void followUpRequestAfterFailure() {
132134
server.verify();
133135
}
134136

137+
@Test // gh-21799
138+
public void verifyShouldFailIfRequestsFailed() {
139+
MockRestServiceServer server = MockRestServiceServer.bindTo(this.restTemplate).build();
140+
server.expect(once(), requestTo("/remoteurl")).andRespond(withSuccess());
141+
142+
this.restTemplate.postForEntity("/remoteurl", null, String.class);
143+
try {
144+
this.restTemplate.postForEntity("/remoteurl", null, String.class);
145+
}
146+
catch (AssertionError error) {
147+
assertThat(error.getMessage()).startsWith("No further requests expected");
148+
}
149+
150+
try {
151+
server.verify();
152+
fail("Expected verify failure");
153+
}
154+
catch (AssertionError error) {
155+
assertThat(error.getMessage()).startsWith("Some requests did not execute successfully");
156+
}
157+
}
135158
}

0 commit comments

Comments
 (0)