Skip to content

Commit 3367df8

Browse files
committed
Suggest use of OkHttp MockWebServer
Issue: SPR-16482
1 parent 7baf33f commit 3367df8

File tree

2 files changed

+44
-26
lines changed

2 files changed

+44
-26
lines changed

src/docs/asciidoc/testing-webtestclient.adoc

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
[[webtestclient]]
22
= WebTestClient
33

4-
`WebTestClient` is a non-blocking, reactive client for testing web servers. It uses
5-
the reactive <<web-reactive.adoc#webflux-webclient, WebClient>> internally to perform
6-
requests and provides a fluent API to verify responses. The `WebTestClient` can connect
7-
to any server over an HTTP connection. It can also bind directly to WebFlux applications
8-
with <<testing.adoc#mock-objects-web-reactive,mock request and response>> objects,
9-
without the need for an HTTP server.
4+
`WebTestClient` is a thin shell around <<web-reactive.adoc#webflux-webclient, WebClient>>,
5+
using it to perform requests and exposing a dedicated, fluent API for verifying responses.
6+
`WebTestClient` bind to a WebFlux application using a
7+
<<testing.adoc#mock-objects-web-reactive,mock request and response>>, or it can test any
8+
web server over an HTTP connection.
9+
1010

1111
[TIP]
1212
====
13-
Kotlin users, please see <<languages.adoc#kotlin-webtestclient-issue,this section>> for
14-
important information on using the `WebTestClient` in Kotlin.
13+
Kotlin users, please see <<languages.adoc#kotlin-webtestclient-issue,this section>>
14+
related to use of the `WebTestClient`.
1515
====
1616

1717

@@ -20,8 +20,8 @@ important information on using the `WebTestClient` in Kotlin.
2020
== Setup
2121

2222
To create a `WebTestClient` you must choose one of several server setup options.
23-
Effectively you either configure a WebFlux application to bind to, or use absolute URLs
24-
to connect to a running server.
23+
Effectively you're either configuring the WebFlux application to bind to, or using
24+
a URL to connect to a running server.
2525

2626

2727

src/docs/asciidoc/web/webflux-webclient.adoc

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,29 @@
11
[[webflux-client]]
22
= WebClient
33

4-
The `spring-webflux` module includes a non-blocking, reactive client for HTTP requests
5-
with Reactive Streams back pressure. It shares
6-
<<web-reactive.adoc#webflux-codecs,HTTP codecs>> and other infrastructure with the
4+
The `spring-webflux` module includes a reactive, non-blocking client for HTTP requests
5+
with a functional-style API client and Reactive Streams support. `WebClient` depends on a
6+
lower level HTTP client library to execute requests and that support is pluggable.
7+
8+
`WebClient`
9+
uses the same <<web-reactive.adoc#webflux-codecs,codecs>> as WebFlux server applications do, and
10+
shares a common base package, some common APIs, and infrastructure with the
711
server <<web-reactive.adoc#webflux-fn,functional web framework>>.
12+
The API exposes Reactor `Flux` and `Mono` types, also see
13+
<<web-reactive.adoc#webflux-reactive-libraries>>. By default it uses
14+
it uses https://github.com/reactor/reactor-netty[Reactor Netty] as the HTTP client
15+
library but others can be plugged in through a custom `ClientHttpConnector`.
816

9-
`WebClient` provides a higher level API over HTTP client libraries. By default
10-
it uses https://github.com/reactor/reactor-netty[Reactor Netty] but that is pluggable
11-
with a different `ClientHttpConnector`. The `WebClient` API returns Reactor `Flux` or
12-
`Mono` for output and accepts Reactive Streams `Publisher` as input (see
13-
<<web-reactive.adoc#webflux-reactive-libraries>>).
17+
By comparison to the <<integration.adoc#rest-resttemplate,RestTemplate>>, the
18+
`WebClient` is:
1419

15-
[TIP]
16-
====
17-
By comparison to the
18-
<<integration.adoc#rest-resttemplate,RestTemplate>>, the `WebClient` offers a more
19-
functional and fluent API that taking full advantage of Java 8 lambdas. It supports both
20-
sync and async scenarios, including streaming, and brings the efficiency of
21-
non-blocking I/O.
22-
====
20+
* non-blocking, reactive, and supports higher concurrency with less hardware resources.
21+
* provides a functional API that takes advantage of Java 8 lambdas.
22+
* supports both synchronous and asynchronous scenarios.
23+
* supports streaming up or down from a server.
24+
25+
For most concurrent scenarios, e.g. a sequence of possibly inter-dependent HTTP calls,
26+
or for making remote calls from the server-side, prefer using the `WebClient`.
2327

2428

2529

@@ -336,3 +340,17 @@ You can also mutate an existing `WebClient` instance without affecting the origi
336340
.filter(basicAuthentication("user", "pwd")
337341
.build();
338342
----
343+
344+
345+
346+
347+
[[webflux-client-testing]]
348+
== Testing
349+
350+
To test code that uses the `WebClient`, you can use a mock web server such as the
351+
https://github.com/square/okhttp#mockwebserver[OkHttp MockWebServer]. To see example
352+
use, check
353+
https://github.com/spring-projects/spring-framework/blob/master/spring-webflux/src/test/java/org/springframework/web/reactive/function/client/WebClientIntegrationTests.java[WebClientIntegrationTests]
354+
in the Spring Framework tests, or the
355+
https://github.com/square/okhttp/tree/master/samples/static-server[static-server]
356+
sample in the OkHttp repository.

0 commit comments

Comments
 (0)