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
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2018 the original author or authors.
|
||||
* Copyright 2002-2019 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.
|
||||
@@ -20,10 +20,13 @@ import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.client.ClientHttpRequest;
|
||||
@@ -49,6 +52,8 @@ public abstract class AbstractRequestExpectationManager implements RequestExpect
|
||||
|
||||
private final List<ClientHttpRequest> requests = new LinkedList<>();
|
||||
|
||||
private final Map<ClientHttpRequest, Throwable> requestFailures = new LinkedHashMap<>();
|
||||
|
||||
|
||||
/**
|
||||
* Return a read-only list of the expectations.
|
||||
@@ -91,6 +96,10 @@ public abstract class AbstractRequestExpectationManager implements RequestExpect
|
||||
expectation = matchRequest(request);
|
||||
}
|
||||
}
|
||||
catch (Throwable ex) {
|
||||
this.requestFailures.put(request, ex);
|
||||
throw ex;
|
||||
}
|
||||
finally {
|
||||
this.requests.add(request);
|
||||
}
|
||||
@@ -129,7 +138,8 @@ public abstract class AbstractRequestExpectationManager implements RequestExpect
|
||||
* @since 5.0.3
|
||||
*/
|
||||
protected RequestExpectation matchRequest(ClientHttpRequest request) throws IOException {
|
||||
throw new UnsupportedOperationException("It looks like neither the deprecated \"validateRequestInternal\"" +
|
||||
throw new UnsupportedOperationException(
|
||||
"It looks like neither the deprecated \"validateRequestInternal\"" +
|
||||
"nor its replacement (this method) are implemented.");
|
||||
}
|
||||
|
||||
@@ -148,6 +158,12 @@ public abstract class AbstractRequestExpectationManager implements RequestExpect
|
||||
String message = "Further request(s) expected leaving " + count + " unsatisfied expectation(s).\n";
|
||||
throw new AssertionError(message + getRequestDetails());
|
||||
}
|
||||
if (!this.requestFailures.isEmpty()) {
|
||||
throw new AssertionError("Some requests did not execute successfully.\n" +
|
||||
this.requestFailures.entrySet().stream()
|
||||
.map(entry -> "Failed request:\n" + entry.getKey() + "\n" + entry.getValue())
|
||||
.collect(Collectors.joining("\n", "\n", "")));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -305,11 +305,15 @@ public final class MockRestServiceServer {
|
||||
}
|
||||
|
||||
@Override
|
||||
public org.springframework.http.client.AsyncClientHttpRequest createAsyncRequest(URI uri, HttpMethod httpMethod) {
|
||||
public org.springframework.http.client.AsyncClientHttpRequest createAsyncRequest(
|
||||
URI uri, HttpMethod httpMethod) {
|
||||
|
||||
return createRequestInternal(uri, httpMethod);
|
||||
}
|
||||
|
||||
private org.springframework.mock.http.client.MockAsyncClientHttpRequest createRequestInternal(URI uri, HttpMethod method) {
|
||||
private org.springframework.mock.http.client.MockAsyncClientHttpRequest createRequestInternal(
|
||||
URI uri, HttpMethod method) {
|
||||
|
||||
Assert.notNull(uri, "'uri' must not be null");
|
||||
Assert.notNull(method, "'httpMethod' must not be null");
|
||||
|
||||
|
||||
Reference in New Issue
Block a user