Skip to content

Commit 00190a1

Browse files
committed
Add exchangeSuccessfully to test clients
Closes gh-35582
1 parent 180ea12 commit 00190a1

File tree

4 files changed

+35
-4
lines changed

4 files changed

+35
-4
lines changed

spring-test/src/main/java/org/springframework/test/web/reactive/server/DefaultWebTestClient.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,11 @@ public ResponseSpec exchange() {
381381
DefaultWebTestClient.this.entityResultConsumer, getResponseTimeout());
382382
}
383383

384+
@Override
385+
public ResponseSpec exchangeSuccessfully() {
386+
return exchange().expectStatus().is2xxSuccessful();
387+
}
388+
384389
private ClientRequest.Builder initRequestBuilder() {
385390
return ClientRequest.create(this.httpMethod, initUri())
386391
.headers(headersToUse -> {

spring-test/src/main/java/org/springframework/test/web/reactive/server/WebTestClient.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -700,10 +700,21 @@ interface RequestHeadersSpec<S extends RequestHeadersSpec<S>> {
700700
S attributes(Consumer<Map<String, Object>> attributesConsumer);
701701

702702
/**
703-
* Perform the exchange without a request body.
704-
* @return a spec for decoding the response
703+
* Perform the exchange.
704+
* @return a spec for expectations on the response
705705
*/
706706
ResponseSpec exchange();
707+
708+
/**
709+
* Variant of {@link #exchange()} that expects a successful response.
710+
* Effectively, a shortcut for:
711+
* <pre class="code">
712+
* exchange().is2xxSuccessful()
713+
* </pre>
714+
* @return a spec for expectations on the response
715+
* @since 7.0
716+
*/
717+
ResponseSpec exchangeSuccessfully();
707718
}
708719

709720

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,11 @@ public ResponseSpec exchange() {
269269
}, false),
270270
DefaultRestTestClient.this.entityResultConsumer);
271271
}
272+
273+
@Override
274+
public ResponseSpec exchangeSuccessfully() {
275+
return exchange().expectStatus().is2xxSuccessful();
276+
}
272277
}
273278

274279

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -514,10 +514,20 @@ interface RequestHeadersSpec<S extends RequestHeadersSpec<S>> {
514514
S attributes(Consumer<Map<String, Object>> attributesConsumer);
515515

516516
/**
517-
* Perform the exchange without a request body.
518-
* @return a spec for decoding the response
517+
* Perform the exchange.
518+
* @return a spec for expectations on the response
519519
*/
520520
ResponseSpec exchange();
521+
522+
/**
523+
* Variant of {@link #exchange()} that expects a successful response.
524+
* Effectively, a shortcut for:
525+
* <pre class="code">
526+
* exchange().is2xxSuccessful()
527+
* </pre>
528+
* @return a spec for expectations on the response
529+
*/
530+
ResponseSpec exchangeSuccessfully();
521531
}
522532

523533

0 commit comments

Comments
 (0)