Skip to content

Commit 34f2597

Browse files
committed
Further alignment of RestTestClient and WebTestClient
See gh-34428
1 parent 2732b60 commit 34f2597

16 files changed

+264
-191
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,12 @@
2929
*/
3030
public class CookieAssertions extends AbstractCookieAssertions<ExchangeResult, WebTestClient.ResponseSpec> {
3131

32-
public CookieAssertions(ExchangeResult exchangeResult, WebTestClient.ResponseSpec responseSpec) {
32+
33+
CookieAssertions(ExchangeResult exchangeResult, WebTestClient.ResponseSpec responseSpec) {
3334
super(exchangeResult, responseSpec);
3435
}
3536

37+
3638
@Override
3739
protected void assertWithDiagnostics(Runnable assertion) {
3840
exchangeResult.assertWithDiagnostics(assertion);

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -438,12 +438,12 @@ private static class DefaultResponseSpec implements ResponseSpec {
438438

439439

440440
DefaultResponseSpec(
441-
ExchangeResult exchangeResult, ClientResponse response,
441+
ExchangeResult result, ClientResponse response,
442442
@Nullable JsonEncoderDecoder jsonEncoderDecoder,
443443
Consumer<EntityExchangeResult<?>> entityResultConsumer,
444444
Duration timeout) {
445445

446-
this.exchangeResult = exchangeResult;
446+
this.exchangeResult = result;
447447
this.response = response;
448448
this.jsonEncoderDecoder = jsonEncoderDecoder;
449449
this.entityResultConsumer = entityResultConsumer;
@@ -468,15 +468,15 @@ public CookieAssertions expectCookie() {
468468
@Override
469469
public <B> BodySpec<B, ?> expectBody(Class<B> bodyType) {
470470
B body = this.response.bodyToMono(bodyType).block(this.timeout);
471-
EntityExchangeResult<B> entityResult = initEntityExchangeResult(body);
472-
return new DefaultBodySpec<>(entityResult);
471+
EntityExchangeResult<B> result = initEntityExchangeResult(body);
472+
return new DefaultBodySpec<>(result);
473473
}
474474

475475
@Override
476476
public <B> BodySpec<B, ?> expectBody(ParameterizedTypeReference<B> bodyType) {
477477
B body = this.response.bodyToMono(bodyType).block(this.timeout);
478-
EntityExchangeResult<B> entityResult = initEntityExchangeResult(body);
479-
return new DefaultBodySpec<>(entityResult);
478+
EntityExchangeResult<B> result = initEntityExchangeResult(body);
479+
return new DefaultBodySpec<>(result);
480480
}
481481

482482
@Override
@@ -500,8 +500,8 @@ private <E> ListBodySpec<E> getListBodySpec(Flux<E> flux) {
500500
public BodyContentSpec expectBody() {
501501
ByteArrayResource resource = this.response.bodyToMono(ByteArrayResource.class).block(this.timeout);
502502
byte[] body = (resource != null ? resource.getByteArray() : null);
503-
EntityExchangeResult<byte[]> entityResult = initEntityExchangeResult(body);
504-
return new DefaultBodyContentSpec(entityResult, this.jsonEncoderDecoder);
503+
EntityExchangeResult<byte[]> result = initEntityExchangeResult(body);
504+
return new DefaultBodyContentSpec(result, this.jsonEncoderDecoder);
505505
}
506506

507507
private <B> EntityExchangeResult<B> initEntityExchangeResult(@Nullable B body) {

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,12 @@
3131
*/
3232
public class HeaderAssertions extends AbstractHeaderAssertions<ExchangeResult, WebTestClient.ResponseSpec> {
3333

34+
3435
HeaderAssertions(ExchangeResult result, WebTestClient.ResponseSpec spec) {
3536
super(result, spec);
3637
}
3738

39+
3840
@Override
3941
protected void assertWithDiagnostics(Runnable assertion) {
4042
exchangeResult.assertWithDiagnostics(assertion);

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,11 @@
3434
*/
3535
public class JsonPathAssertions extends AbstractJsonPathAssertions<WebTestClient.BodyContentSpec> {
3636

37-
JsonPathAssertions(WebTestClient.BodyContentSpec spec, String content, String expression,
37+
38+
JsonPathAssertions(
39+
WebTestClient.BodyContentSpec spec, String content, String expression,
3840
@Nullable Configuration configuration) {
41+
3942
super(spec, content, expression, configuration);
4043
}
4144
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,12 @@
2929
*/
3030
public class StatusAssertions extends AbstractStatusAssertions<ExchangeResult, WebTestClient.ResponseSpec> {
3131

32+
3233
StatusAssertions(ExchangeResult result, WebTestClient.ResponseSpec spec) {
3334
super(result, spec);
3435
}
3536

37+
3638
@Override
3739
protected void assertWithDiagnostics(Runnable assertion) {
3840
exchangeResult.assertWithDiagnostics(assertion);

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

Lines changed: 20 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@
7373
* Client for testing web servers that uses {@link WebClient} internally to
7474
* perform requests while also providing a fluent API to verify responses.
7575
* This client can connect to any server over HTTP, or to a WebFlux application
76-
* via mock request and response objects.
76+
* with a mock request and response.
7777
*
7878
* <p>Use one of the bindToXxx methods to create an instance. For example:
7979
* <ul>
@@ -89,9 +89,6 @@
8989
* @author Sam Brannen
9090
* @author Michał Rowicki
9191
* @since 5.0
92-
* @see StatusAssertions
93-
* @see HeaderAssertions
94-
* @see JsonPathAssertions
9592
*/
9693
public interface WebTestClient {
9794

@@ -172,12 +169,10 @@ public interface WebTestClient {
172169

173170

174171
/**
175-
* Use this server setup to test one {@code @Controller} at a time.
176-
* This option loads the default configuration of
177-
* {@link org.springframework.web.reactive.config.EnableWebFlux @EnableWebFlux}.
178-
* There are builder methods to customize the Java config. The resulting
179-
* WebFlux application will be tested without an HTTP server using a mock
180-
* request and response.
172+
* Begin creating a {@link WebTestClient} with a mock server setup that
173+
* tests one {@code @Controller} at a time with
174+
* {@link org.springframework.web.reactive.config.EnableWebFlux @EnableWebFlux}
175+
* equivalent configuration.
181176
* @param controllers one or more controller instances to test
182177
* (specified {@code Class} will be turned into instance)
183178
* @return chained API to customize server and client config; use
@@ -188,10 +183,10 @@ static ControllerSpec bindToController(Object... controllers) {
188183
}
189184

190185
/**
191-
* Use this option to set up a server from a {@link RouterFunction}.
192-
* Internally the provided configuration is passed to
193-
* {@code RouterFunctions#toWebHandler}. The resulting WebFlux application
194-
* will be tested without an HTTP server using a mock request and response.
186+
* Begin creating a {@link WebTestClient} with a mock server setup that
187+
* tests one {@code RouterFunction} at a time with
188+
* {@link org.springframework.web.reactive.config.EnableWebFlux @EnableWebFlux}
189+
* equivalent configuration.
195190
* @param routerFunction the RouterFunction to test
196191
* @return chained API to customize server and client config; use
197192
* {@link MockServerSpec#configureClient()} to transition to client config
@@ -229,8 +224,7 @@ static MockServerSpec<?> bindToWebHandler(WebHandler webHandler) {
229224
}
230225

231226
/**
232-
* This server setup option allows you to connect to a live server through
233-
* a Reactor Netty client connector.
227+
* This server setup option allows you to connect to a live server.
234228
* <p><pre class="code">
235229
* WebTestClient client = WebTestClient.bindToServer()
236230
* .baseUrl("http://localhost:8080")
@@ -389,17 +383,12 @@ interface RouterFunctionSpec extends MockServerSpec<RouterFunctionSpec> {
389383

390384

391385
/**
392-
* Steps for customizing the {@link WebClient} used to test with,
393-
* internally delegating to a
394-
* {@link org.springframework.web.reactive.function.client.WebClient.Builder
395-
* WebClient.Builder}.
386+
* Steps to customize the underlying {@link WebClient} via {@link WebClient.Builder}.
396387
*/
397388
interface Builder {
398389

399390
/**
400-
* Configure a base URI as described in
401-
* {@link org.springframework.web.reactive.function.client.WebClient#create(String)
402-
* WebClient.create(String)}.
391+
* Configure a base URI as described in {@link WebClient#create(String)}.
403392
*/
404393
Builder baseUrl(String baseUrl);
405394

@@ -428,7 +417,7 @@ interface Builder {
428417
Builder defaultHeaders(Consumer<HttpHeaders> headersConsumer);
429418

430419
/**
431-
* Add the given header to all requests that haven't added it.
420+
* Add the given cookie to all requests that haven't already added it.
432421
* @param cookieName the cookie name
433422
* @param cookieValues the cookie values
434423
*/
@@ -718,6 +707,7 @@ interface RequestHeadersSpec<S extends RequestHeadersSpec<S>> {
718707
* Specification for providing body of a request.
719708
*/
720709
interface RequestBodySpec extends RequestHeadersSpec<RequestBodySpec> {
710+
721711
/**
722712
* Set the length of the body in bytes, as specified by the
723713
* {@code Content-Length} header.
@@ -738,7 +728,7 @@ interface RequestBodySpec extends RequestHeadersSpec<RequestBodySpec> {
738728

739729
/**
740730
* Set the body to the given {@code Object} value. This method invokes the
741-
* {@link org.springframework.web.reactive.function.client.WebClient.RequestBodySpec#bodyValue(Object)
731+
* {@link WebClient.RequestBodySpec#bodyValue(Object)
742732
* bodyValue} method on the underlying {@code WebClient}.
743733
* @param body the value to write to the request body
744734
* @return spec for further declaration of the request
@@ -773,7 +763,7 @@ <T, S extends Publisher<T>> RequestHeadersSpec<?> body(
773763

774764
/**
775765
* Set the body from the given producer. This method invokes the
776-
* {@link org.springframework.web.reactive.function.client.WebClient.RequestBodySpec#body(Object, Class)
766+
* {@link WebClient.RequestBodySpec#body(Object, Class)
777767
* body(Object, Class)} method on the underlying {@code WebClient}.
778768
* @param producer the producer to write to the request. This must be a
779769
* {@link Publisher} or another producer adaptable to a
@@ -786,7 +776,7 @@ <T, S extends Publisher<T>> RequestHeadersSpec<?> body(
786776

787777
/**
788778
* Set the body from the given producer. This method invokes the
789-
* {@link org.springframework.web.reactive.function.client.WebClient.RequestBodySpec#body(Object, ParameterizedTypeReference)
779+
* {@link WebClient.RequestBodySpec#body(Object, ParameterizedTypeReference)
790780
* body(Object, ParameterizedTypeReference)} method on the underlying {@code WebClient}.
791781
* @param producer the producer to write to the request. This must be a
792782
* {@link Publisher} or another producer adaptable to a
@@ -800,7 +790,7 @@ <T, S extends Publisher<T>> RequestHeadersSpec<?> body(
800790
/**
801791
* Set the body of the request to the given {@code BodyInserter}.
802792
* This method invokes the
803-
* {@link org.springframework.web.reactive.function.client.WebClient.RequestBodySpec#body(BodyInserter)
793+
* {@link WebClient.RequestBodySpec#body(BodyInserter)
804794
* body(BodyInserter)} method on the underlying {@code WebClient}.
805795
* @param inserter the body inserter to use
806796
* @return spec for further declaration of the request
@@ -908,8 +898,8 @@ interface ResponseSpec {
908898
BodyContentSpec expectBody();
909899

910900
/**
911-
* Exit the chained flow in order to consume the response body
912-
* externally, for example, via {@link reactor.test.StepVerifier}.
901+
* Exit the chained flow in order to consume the response body externally,
902+
* for example, via {@link reactor.test.StepVerifier}.
913903
* <p>Note that when {@code Void.class} is passed in, the response body
914904
* is consumed and released. If no content is expected, then consider
915905
* using {@code .expectBody().isEmpty()} instead which asserts that

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,12 @@
2828
*/
2929
public class CookieAssertions extends AbstractCookieAssertions<ExchangeResult, RestTestClient.ResponseSpec> {
3030

31-
public CookieAssertions(ExchangeResult exchangeResult, RestTestClient.ResponseSpec responseSpec) {
31+
32+
CookieAssertions(ExchangeResult exchangeResult, RestTestClient.ResponseSpec responseSpec) {
3233
super(exchangeResult, responseSpec);
3334
}
3435

36+
3537
@Override
3638
protected void assertWithDiagnostics(Runnable assertion) {
3739
exchangeResult.assertWithDiagnostics(assertion);

0 commit comments

Comments
 (0)