Skip to content

Commit 95ff22c

Browse files
Jay Bryantbclozel
authored andcommitted
Edit the testing part of the reference documentation
I edited for spelling, punctuation, grammar, usage, and corporate voice. I also added cross-references and links to the Javadoc.
1 parent 395e3d0 commit 95ff22c

File tree

3 files changed

+2274
-1603
lines changed

3 files changed

+2274
-1603
lines changed

src/docs/asciidoc/core/core-beans.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8420,7 +8420,7 @@ the `Environment` or ,declaratively, by using the `spring.profiles.default` prop
84208420

84218421

84228422
[[beans-property-source-abstraction]]
8423-
=== `PropertySource` abstraction
8423+
=== `PropertySource` Abstraction
84248424

84258425
Spring's `Environment` abstraction provides search operations over a configurable
84268426
hierarchy of property sources. Consider the following listing:

src/docs/asciidoc/testing-webtestclient.adoc

Lines changed: 88 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,12 @@
33

44
`WebTestClient` is a thin shell around <<web-reactive.adoc#webflux-webclient, WebClient>>,
55
using it to perform requests and exposing a dedicated, fluent API for verifying responses.
6-
`WebTestClient` bind to a WebFlux application using a
6+
`WebTestClient` binds to a WebFlux application by using a
77
<<testing.adoc#mock-objects-web-reactive,mock request and response>>, or it can test any
88
web server over an HTTP connection.
99

10-
11-
[TIP]
12-
====
13-
Kotlin users, please see <<languages.adoc#kotlin-webtestclient-issue,this section>>
10+
TIP: Kotlin users: See <<languages.adoc#kotlin-webtestclient-issue,this section>>
1411
related to use of the `WebTestClient`.
15-
====
1612

1713

1814

@@ -26,48 +22,53 @@ a URL to connect to a running server.
2622

2723

2824
[[webtestclient-controller-config]]
29-
=== Bind to controller
25+
=== Bind to Controller
3026

31-
Use this server setup to test one `@Controller` at a time:
27+
The following example shows how to create a server setup to test one `@Controller` at a time:
3228

29+
====
3330
[source,java,intent=0]
3431
[subs="verbatim,quotes"]
3532
----
3633
client = WebTestClient.bindToController(new TestController()).build();
3734
----
35+
====
3836

39-
The above loads the <<web-reactive.adoc#webflux-config,WebFlux Java config>> and
40-
registers the given controller. The resulting WebFlux application will be tested
41-
without an HTTP server using mock request and response objects. There are more methods
42-
on the builder to customize the default WebFlux Java config.
37+
The preceding example loads the <<web-reactive.adoc#webflux-config,WebFlux Java configuration>> and
38+
registers the given controller. The resulting WebFlux application is tested
39+
without an HTTP server by using mock request and response objects. There are more methods
40+
on the builder to customize the default WebFlux Java configuration.
4341

4442

4543

4644
[[webtestclient-fn-config]]
47-
=== Bind to RouterFunction
45+
=== Bind to Router Function
4846

49-
Use this option to set up a server from a
47+
The folloiwng example shows how to set up a server from a
5048
<<web-reactive.adoc#webflux-fn,RouterFunction>>:
5149

50+
====
5251
[source,java,intent=0]
5352
[subs="verbatim,quotes"]
5453
----
5554
RouterFunction<?> route = ...
5655
client = WebTestClient.bindToRouterFunction(route).build();
5756
----
57+
====
5858

59-
Internally the provided configuration is passed to `RouterFunctions.toWebHandler`.
60-
The resulting WebFlux application will be tested without an HTTP server using mock
59+
Internally, the configuration is passed to `RouterFunctions.toWebHandler`.
60+
The resulting WebFlux application is tested without an HTTP server by using mock
6161
request and response objects.
6262

6363

6464

6565
[[webtestclient-context-config]]
66-
=== Bind to ApplicationContext
66+
=== Bind to `ApplicationContext`
6767

68-
Use this option to setup a server from the Spring configuration of your application, or
68+
The following example shows how to setup a server from the Spring configuration of your application or
6969
some subset of it:
7070

71+
====
7172
[source,java,intent=0]
7273
[subs="verbatim,quotes"]
7374
----
@@ -90,35 +91,40 @@ some subset of it:
9091
<1> Specify the configuration to load
9192
<2> Inject the configuration
9293
<3> Create the `WebTestClient`
94+
====
9395

94-
Internally the provided configuration is passed to `WebHttpHandlerBuilder` to set up
95-
the request processing chain, see
96+
Internally, the configuration is passed to `WebHttpHandlerBuilder` to set up
97+
the request processing chain. See
9698
<<web-reactive.adoc#webflux-web-handler-api,WebHandler API>> for more details. The
97-
resulting WebFlux application will be tested without an HTTP server using mock request
99+
resulting WebFlux application is tested without an HTTP server by using mock request
98100
and response objects.
99101

100102

101103

102104
[[webtestclient-server-config]]
103-
=== Bind to server
105+
=== Bind to Server
104106

105-
This server setup option allows you to connect to a running server:
107+
The following server setup option lets you connect to a running server:
106108

109+
====
107110
[source,java,intent=0]
108111
[subs="verbatim,quotes"]
109112
----
110113
client = WebTestClient.bindToServer().baseUrl("http://localhost:8080").build();
111114
----
115+
====
116+
112117

113118

114119
[[webtestclient-client-config]]
115-
=== Client builder
120+
=== Client Builder
116121

117-
In addition to the server setup options above, you can also configure client
118-
options including base URL, default headers, client filters, and others. These options
122+
In addition to the server setup options described earlier, you can also configure client
123+
options, including base URL, default headers, client filters, and others. These options
119124
are readily available following `bindToServer`. For all others, you need to use
120-
`configureClient()` to transition from server to client configuration as shown below:
125+
`configureClient()` to transition from server to client configuration, as follows:
121126

127+
====
122128
[source,java,intent=0]
123129
[subs="verbatim,quotes"]
124130
----
@@ -127,19 +133,20 @@ are readily available following `bindToServer`. For all others, you need to use
127133
.baseUrl("/test")
128134
.build();
129135
----
130-
136+
====
131137

132138

133139

134140
[[webtestclient-tests]]
135-
== Writing tests
141+
== Writing Tests
136142

137143
`WebTestClient` is a thin shell around <<web-reactive.adoc#webflux-webclient,WebClient>>.
138-
It provides an identical API up to the point of performing a request via `exchange()`.
144+
It provides an identical API up to the point of performing a request by using `exchange()`.
139145
What follows after `exchange()` is a chained API workflow to verify responses.
140146

141-
Typically you start by asserting the response status and headers:
147+
Typically, you start by asserting the response status and headers, as follows:
142148

149+
====
143150
[source,java,intent=0]
144151
[subs="verbatim,quotes"]
145152
----
@@ -150,15 +157,17 @@ Typically you start by asserting the response status and headers:
150157
.expectHeader().contentType(MediaType.APPLICATION_JSON_UTF8)
151158
// ...
152159
----
160+
====
153161

154162
Then you specify how to decode and consume the response body:
155163

156-
* `expectBody(Class<T>)` -- decode to single object.
157-
* `expectBodyList(Class<T>)` -- decode and collect objects to `List<T>`.
158-
* `expectBody()` -- decode to `byte[]` for <<webtestclient-json>> or empty body.
164+
* `expectBody(Class<T>)`: Decode to single object.
165+
* `expectBodyList(Class<T>)`: Decode and collect objects to `List<T>`.
166+
* `expectBody()`: Decode to `byte[]` for <<webtestclient-json>> or an empty body.
159167

160-
Then you can use built-in assertions for the body. Here is one example:
168+
Then you can use built-in assertions for the body. The following example shows one way to do so:
161169

170+
====
162171
[source,java,intent=0]
163172
[subs="verbatim,quotes"]
164173
----
@@ -167,9 +176,11 @@ Then you can use built-in assertions for the body. Here is one example:
167176
.expectStatus().isOk()
168177
.expectBodyList(Person.class).hasSize(3).contains(person);
169178
----
179+
====
170180

171-
You can go beyond the built-in assertions and create your own:
181+
You can also go beyond the built-in assertions and create your own, as the following example shows:
172182

183+
====
173184
----
174185
client.get().uri("/persons/1")
175186
.exchange()
@@ -179,32 +190,34 @@ You can go beyond the built-in assertions and create your own:
179190
// custom assertions (e.g. AssertJ)...
180191
});
181192
----
193+
====
182194

183-
You can also exit the workflow and get a result:
195+
You can also exit the workflow and get a result, as follows:
184196

197+
====
185198
----
186199
EntityExchangeResult<Person> result = client.get().uri("/persons/1")
187200
.exchange()
188201
.expectStatus().isOk()
189202
.expectBody(Person.class)
190203
.returnResult();
191204
----
192-
193-
[TIP]
194205
====
195-
When you need to decode to a target type with generics, look for the overloaded methods
206+
207+
TIP: When you need to decode to a target type with generics, look for the overloaded methods
196208
that accept
197-
{api-spring-framework}/core/ParameterizedTypeReference.html[ParameterizedTypeReference]
209+
{api-spring-framework}/core/ParameterizedTypeReference.html[`ParameterizedTypeReference`]
198210
instead of `Class<T>`.
199-
====
211+
200212

201213

202214
[[webtestclient-no-content]]
203-
=== No content
215+
=== No Content
204216

205-
If the response has no content, or you don't care if it does, use `Void.class` which ensures
206-
that resources are released:
217+
If the response has no content (or you do not care if it does) use `Void.class`, which ensures
218+
that resources are released. The following example shows how to do so:
207219

220+
====
208221
[source,java,intent=0]
209222
[subs="verbatim,quotes"]
210223
----
@@ -213,9 +226,11 @@ that resources are released:
213226
.expectStatus().isNotFound()
214227
.expectBody(Void.class);
215228
----
229+
====
216230

217-
Or if you want to assert there is no response content, use this:
231+
Alternatively, if you want to assert there is no response content, you can use code similar to the following:
218232

233+
====
219234
[source,java,intent=0]
220235
[subs="verbatim,quotes"]
221236
----
@@ -225,16 +240,18 @@ Or if you want to assert there is no response content, use this:
225240
.expectStatus().isCreated()
226241
.expectBody().isEmpty();
227242
----
243+
====
228244

229245

230246

231247
[[webtestclient-json]]
232-
=== JSON content
248+
=== JSON Content
233249

234-
When you use `expectBody()` the response is consumed as a `byte[]`. This is useful for
235-
raw content assertions. For example you can use
236-
http://jsonassert.skyscreamer.org[JSONAssert] to verify JSON content:
250+
When you use `expectBody()`, the response is consumed as a `byte[]`. This is useful for
251+
raw content assertions. For example, you can use
252+
http://jsonassert.skyscreamer.org[JSONAssert] to verify JSON content, as follows:
237253

254+
====
238255
[source,java,intent=0]
239256
[subs="verbatim,quotes"]
240257
----
@@ -244,9 +261,11 @@ http://jsonassert.skyscreamer.org[JSONAssert] to verify JSON content:
244261
.expectBody()
245262
.json("{\"name\":\"Jane\"}")
246263
----
264+
====
247265

248-
You can also use https://github.com/jayway/JsonPath[JSONPath] expressions:
266+
You can also use https://github.com/jayway/JsonPath[JSONPath] expressions, as follows:
249267

268+
====
250269
[source,java,intent=0]
251270
[subs="verbatim,quotes"]
252271
----
@@ -257,15 +276,18 @@ You can also use https://github.com/jayway/JsonPath[JSONPath] expressions:
257276
.jsonPath("$[0].name").isEqualTo("Jane")
258277
.jsonPath("$[1].name").isEqualTo("Jason");
259278
----
279+
====
280+
260281

261282

262283
[[webtestclient-stream]]
263-
=== Streaming responses
284+
=== Streaming Responses
264285

265-
To test infinite streams (e.g. `"text/event-stream"`, `"application/stream+json"`),
266-
you'll need to exit the chained API, via `returnResult`, immediately after response status
267-
and header assertions, as shown below:
286+
To test infinite streams (for example, `"text/event-stream"` or `"application/stream+json"`),
287+
you need to exit the chained API (by using `returnResult`), immediately after the response status
288+
and header assertions, as the following example shows:
268289

290+
====
269291
[source,java,intent=0]
270292
[subs="verbatim,quotes"]
271293
----
@@ -276,11 +298,13 @@ and header assertions, as shown below:
276298
.returnResult(MyEvent.class);
277299
278300
----
301+
====
279302

280303
Now you can consume the `Flux<T>`, assert decoded objects as they come, and then
281-
cancel at some point when test objects are met. We recommend using the `StepVerifier`
282-
from the `reactor-test` module to do that, for example:
304+
cancel at some point when test objectives are met. We recommend using the `StepVerifier`
305+
from the `reactor-test` module to do that, as the following example shows:
283306

307+
====
284308
[source,java,intent=0]
285309
[subs="verbatim,quotes"]
286310
----
@@ -293,13 +317,15 @@ from the `reactor-test` module to do that, for example:
293317
.thenCancel()
294318
.verify();
295319
----
320+
====
321+
296322

297323

298324
[[webtestclient-request-body]]
299-
=== Request body
325+
=== Request Body
300326

301-
When it comes to building requests, the `WebTestClient` offers an identical API as the
302-
`WebClient` and the implementation is mostly a simple pass-through. Please refer
303-
to the <<web-reactive.adoc#webflux-client-body,WebClient documentation>> for examples on
304-
how to prepare a request with a body including submitting form data, multipart requests,
327+
When it comes to building requests, the `WebTestClient` offers an API identical to the
328+
`WebClient`, and the implementation is mostly a simple pass-through. See
329+
the <<web-reactive.adoc#webflux-client-body,WebClient documentation>> for examples on
330+
how to prepare a request with a body, including submitting form data, multipart requests,
305331
and more.

0 commit comments

Comments
 (0)