Improve support of async request in MockMvcTester

This commit improves the handling of asynchronous requests by offering
a way to opt-in for the raw async result. This provides first class
support for asserting a request that might still be in process as well
as the asyncResult, if necessary.

See gh-33040
This commit is contained in:
Stéphane Nicoll
2024-06-18 13:35:33 +02:00
parent a7503e7200
commit 24bbc6d80d
3 changed files with 80 additions and 10 deletions

View File

@@ -448,6 +448,8 @@ public final class MockMvcTester {
* assertThat(mvc.get().uri("/greet")).hasStatusOk();
* assertThat(mvc.get().uri("/greet").exchange()).hasStatusOk();
* </code></pre>
* <p>For assertions on the original asynchronous request that might
* still be in progress, use {@link #asyncExchange()}.
* @see #exchange(Duration) to customize the timeout for async requests
*/
public MvcTestResult exchange() {
@@ -458,12 +460,23 @@ public final class MockMvcTester {
* Execute the request and wait at most the given {@code timeToWait}
* duration for the asynchronous request to complete. If the request
* is not asynchronous, the {@code timeToWait} is ignored.
* <p>For assertions on the original asynchronous request that might
* still be in progress, use {@link #asyncExchange()}.
* @see #exchange()
*/
public MvcTestResult exchange(Duration timeToWait) {
return MockMvcTester.this.exchange(this, timeToWait);
}
/**
* Execute the request and do not attempt to wait for the completion of
* an asynchronous request. Contrary to {@link #exchange()}, this returns
* the original result that might still be in progress.
*/
public MvcTestResult asyncExchange() {
return MockMvcTester.this.perform(this);
}
@Override
public MvcTestResultAssert assertThat() {
return new MvcTestResultAssert(exchange(), MockMvcTester.this.jsonMessageConverter);
@@ -493,6 +506,8 @@ public final class MockMvcTester {
* assertThat(mvc.get().uri("/greet")).hasStatusOk();
* assertThat(mvc.get().uri("/greet").exchange()).hasStatusOk();
* </code></pre>
* <p>For assertions on the original asynchronous request that might
* still be in progress, use {@link #asyncExchange()}.
* @see #exchange(Duration) to customize the timeout for async requests
*/
public MvcTestResult exchange() {
@@ -503,12 +518,23 @@ public final class MockMvcTester {
* Execute the request and wait at most the given {@code timeToWait}
* duration for the asynchronous request to complete. If the request
* is not asynchronous, the {@code timeToWait} is ignored.
* <p>For assertions on the original asynchronous request that might
* still be in progress, use {@link #asyncExchange()}.
* @see #exchange()
*/
public MvcTestResult exchange(Duration timeToWait) {
return MockMvcTester.this.exchange(this, timeToWait);
}
/**
* Execute the request and do not attempt to wait for the completion of
* an asynchronous request. Contrary to {@link #exchange()}, this returns
* the original result that might still be in progress.
*/
public MvcTestResult asyncExchange() {
return MockMvcTester.this.perform(this);
}
@Override
public MvcTestResultAssert assertThat() {
return new MvcTestResultAssert(exchange(), MockMvcTester.this.jsonMessageConverter);