Add verify(Duration) to MockRestServiceServer
Closes gh-22618
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2020 the original author or authors.
|
||||
* Copyright 2002-2021 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.
|
||||
@@ -18,6 +18,8 @@ package org.springframework.test.web.client;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
@@ -144,25 +146,42 @@ public abstract class AbstractRequestExpectationManager implements RequestExpect
|
||||
|
||||
@Override
|
||||
public void verify() {
|
||||
if (this.expectations.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
int count = 0;
|
||||
for (RequestExpectation expectation : this.expectations) {
|
||||
if (!expectation.isSatisfied()) {
|
||||
count++;
|
||||
}
|
||||
}
|
||||
int count = verifyInternal();
|
||||
if (count > 0) {
|
||||
String message = "Further request(s) expected leaving " + count + " unsatisfied expectation(s).\n";
|
||||
throw new AssertionError(message + getRequestDetails());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void verify(Duration timeout) {
|
||||
Instant endTime = Instant.now().plus(timeout);
|
||||
do {
|
||||
if (verifyInternal() == 0) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
while (Instant.now().isBefore(endTime));
|
||||
verify();
|
||||
}
|
||||
|
||||
private int verifyInternal() {
|
||||
if (this.expectations.isEmpty()) {
|
||||
return 0;
|
||||
}
|
||||
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", "")));
|
||||
}
|
||||
int count = 0;
|
||||
for (RequestExpectation expectation : this.expectations) {
|
||||
if (!expectation.isSatisfied()) {
|
||||
count++;
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2019 the original author or authors.
|
||||
* Copyright 2002-2021 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.
|
||||
@@ -18,6 +18,7 @@ package org.springframework.test.web.client;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.time.Duration;
|
||||
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.client.BufferingClientHttpRequestFactory;
|
||||
@@ -110,12 +111,25 @@ public final class MockRestServiceServer {
|
||||
/**
|
||||
* Verify that all expected requests set up via
|
||||
* {@link #expect(RequestMatcher)} were indeed performed.
|
||||
* @throws AssertionError when some expectations were not met
|
||||
* @throws AssertionError if not all expectations are met
|
||||
*/
|
||||
public void verify() {
|
||||
this.expectationManager.verify();
|
||||
}
|
||||
|
||||
/**
|
||||
* Variant of {@link #verify()} that waits for up to the specified time for
|
||||
* all expectations to be fulfilled. This can be useful for tests that
|
||||
* involve asynchronous requests.
|
||||
* @param timeout how long to wait for all expecations to be met
|
||||
* @throws AssertionError if not all expectations are met by the specified
|
||||
* timeout, or if any expectation fails at any time before that.
|
||||
* @since 5.3.4
|
||||
*/
|
||||
public void verify(Duration timeout) {
|
||||
this.expectationManager.verify(timeout);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reset the internal state removing all expectations and recorded requests.
|
||||
*/
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2018 the original author or authors.
|
||||
* Copyright 2002-2021 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.
|
||||
@@ -17,6 +17,7 @@
|
||||
package org.springframework.test.web.client;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.time.Duration;
|
||||
|
||||
import org.springframework.http.client.ClientHttpRequest;
|
||||
import org.springframework.http.client.ClientHttpResponse;
|
||||
@@ -52,11 +53,22 @@ public interface RequestExpectationManager {
|
||||
/**
|
||||
* Verify that all expectations have been met.
|
||||
* <p>This is a delegate for {@link MockRestServiceServer#verify()}.
|
||||
* @throws AssertionError when some expectations were not met
|
||||
* @throws AssertionError if not all expectations are met
|
||||
* @see MockRestServiceServer#verify()
|
||||
*/
|
||||
void verify();
|
||||
|
||||
/**
|
||||
* Variant of {@link #verify()} that waits for up to the specified time for
|
||||
* all expectations to be fulfilled. This can be useful for tests that
|
||||
* involve asynchronous requests.
|
||||
* @param timeout how long to wait for all expecations to be met
|
||||
* @throws AssertionError if not all expectations are met by the specified
|
||||
* timeout, or if any expectation fails at any time before that.
|
||||
* @since 5.3.4
|
||||
*/
|
||||
void verify(Duration timeout);
|
||||
|
||||
/**
|
||||
* Reset the internal state removing all expectations and recorded requests.
|
||||
* <p>This is a delegate for {@link MockRestServiceServer#reset()}.
|
||||
@@ -75,5 +87,4 @@ public interface RequestExpectationManager {
|
||||
* @throws IOException in case of any validation errors
|
||||
*/
|
||||
ClientHttpResponse validateRequest(ClientHttpRequest request) throws IOException;
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user