3
3
4
4
`WebTestClient` is a thin shell around <<web-reactive.adoc#webflux-webclient, WebClient>>,
5
5
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
7
7
<<testing.adoc#mock-objects-web-reactive,mock request and response>>, or it can test any
8
8
web server over an HTTP connection.
9
9
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>>
14
11
related to use of the `WebTestClient`.
15
- ====
16
12
17
13
18
14
@@ -26,48 +22,53 @@ a URL to connect to a running server.
26
22
27
23
28
24
[[webtestclient-controller-config]]
29
- === Bind to controller
25
+ === Bind to Controller
30
26
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:
32
28
29
+ ====
33
30
[source,java,intent=0]
34
31
[subs="verbatim,quotes"]
35
32
----
36
33
client = WebTestClient.bindToController(new TestController()).build();
37
34
----
35
+ ====
38
36
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 .
43
41
44
42
45
43
46
44
[[webtestclient-fn-config]]
47
- === Bind to RouterFunction
45
+ === Bind to Router Function
48
46
49
- Use this option to set up a server from a
47
+ The folloiwng example shows how to set up a server from a
50
48
<<web-reactive.adoc#webflux-fn,RouterFunction>>:
51
49
50
+ ====
52
51
[source,java,intent=0]
53
52
[subs="verbatim,quotes"]
54
53
----
55
54
RouterFunction<?> route = ...
56
55
client = WebTestClient.bindToRouterFunction(route).build();
57
56
----
57
+ ====
58
58
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
61
61
request and response objects.
62
62
63
63
64
64
65
65
[[webtestclient-context-config]]
66
- === Bind to ApplicationContext
66
+ === Bind to ` ApplicationContext`
67
67
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
69
69
some subset of it:
70
70
71
+ ====
71
72
[source,java,intent=0]
72
73
[subs="verbatim,quotes"]
73
74
----
@@ -90,35 +91,40 @@ some subset of it:
90
91
<1> Specify the configuration to load
91
92
<2> Inject the configuration
92
93
<3> Create the `WebTestClient`
94
+ ====
93
95
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
96
98
<<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
98
100
and response objects.
99
101
100
102
101
103
102
104
[[webtestclient-server-config]]
103
- === Bind to server
105
+ === Bind to Server
104
106
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:
106
108
109
+ ====
107
110
[source,java,intent=0]
108
111
[subs="verbatim,quotes"]
109
112
----
110
113
client = WebTestClient.bindToServer().baseUrl("http://localhost:8080").build();
111
114
----
115
+ ====
116
+
112
117
113
118
114
119
[[webtestclient-client-config]]
115
- === Client builder
120
+ === Client Builder
116
121
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
119
124
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 :
121
126
127
+ ====
122
128
[source,java,intent=0]
123
129
[subs="verbatim,quotes"]
124
130
----
@@ -127,19 +133,20 @@ are readily available following `bindToServer`. For all others, you need to use
127
133
.baseUrl("/test")
128
134
.build();
129
135
----
130
-
136
+ ====
131
137
132
138
133
139
134
140
[[webtestclient-tests]]
135
- == Writing tests
141
+ == Writing Tests
136
142
137
143
`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()`.
139
145
What follows after `exchange()` is a chained API workflow to verify responses.
140
146
141
- Typically you start by asserting the response status and headers:
147
+ Typically, you start by asserting the response status and headers, as follows :
142
148
149
+ ====
143
150
[source,java,intent=0]
144
151
[subs="verbatim,quotes"]
145
152
----
@@ -150,15 +157,17 @@ Typically you start by asserting the response status and headers:
150
157
.expectHeader().contentType(MediaType.APPLICATION_JSON_UTF8)
151
158
// ...
152
159
----
160
+ ====
153
161
154
162
Then you specify how to decode and consume the response body:
155
163
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.
159
167
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 :
161
169
170
+ ====
162
171
[source,java,intent=0]
163
172
[subs="verbatim,quotes"]
164
173
----
@@ -167,9 +176,11 @@ Then you can use built-in assertions for the body. Here is one example:
167
176
.expectStatus().isOk()
168
177
.expectBodyList(Person.class).hasSize(3).contains(person);
169
178
----
179
+ ====
170
180
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 :
172
182
183
+ ====
173
184
----
174
185
client.get().uri("/persons/1")
175
186
.exchange()
@@ -179,32 +190,34 @@ You can go beyond the built-in assertions and create your own:
179
190
// custom assertions (e.g. AssertJ)...
180
191
});
181
192
----
193
+ ====
182
194
183
- You can also exit the workflow and get a result:
195
+ You can also exit the workflow and get a result, as follows :
184
196
197
+ ====
185
198
----
186
199
EntityExchangeResult<Person> result = client.get().uri("/persons/1")
187
200
.exchange()
188
201
.expectStatus().isOk()
189
202
.expectBody(Person.class)
190
203
.returnResult();
191
204
----
192
-
193
- [TIP]
194
205
====
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
196
208
that accept
197
- {api-spring-framework}/core/ParameterizedTypeReference.html[ParameterizedTypeReference]
209
+ {api-spring-framework}/core/ParameterizedTypeReference.html[` ParameterizedTypeReference` ]
198
210
instead of `Class<T>`.
199
- ====
211
+
200
212
201
213
202
214
[[webtestclient-no-content]]
203
- === No content
215
+ === No Content
204
216
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 :
207
219
220
+ ====
208
221
[source,java,intent=0]
209
222
[subs="verbatim,quotes"]
210
223
----
@@ -213,9 +226,11 @@ that resources are released:
213
226
.expectStatus().isNotFound()
214
227
.expectBody(Void.class);
215
228
----
229
+ ====
216
230
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 :
218
232
233
+ ====
219
234
[source,java,intent=0]
220
235
[subs="verbatim,quotes"]
221
236
----
@@ -225,16 +240,18 @@ Or if you want to assert there is no response content, use this:
225
240
.expectStatus().isCreated()
226
241
.expectBody().isEmpty();
227
242
----
243
+ ====
228
244
229
245
230
246
231
247
[[webtestclient-json]]
232
- === JSON content
248
+ === JSON Content
233
249
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 :
237
253
254
+ ====
238
255
[source,java,intent=0]
239
256
[subs="verbatim,quotes"]
240
257
----
@@ -244,9 +261,11 @@ http://jsonassert.skyscreamer.org[JSONAssert] to verify JSON content:
244
261
.expectBody()
245
262
.json("{\"name\":\"Jane\"}")
246
263
----
264
+ ====
247
265
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 :
249
267
268
+ ====
250
269
[source,java,intent=0]
251
270
[subs="verbatim,quotes"]
252
271
----
@@ -257,15 +276,18 @@ You can also use https://github.com/jayway/JsonPath[JSONPath] expressions:
257
276
.jsonPath("$[0].name").isEqualTo("Jane")
258
277
.jsonPath("$[1].name").isEqualTo("Jason");
259
278
----
279
+ ====
280
+
260
281
261
282
262
283
[[webtestclient-stream]]
263
- === Streaming responses
284
+ === Streaming Responses
264
285
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 :
268
289
290
+ ====
269
291
[source,java,intent=0]
270
292
[subs="verbatim,quotes"]
271
293
----
@@ -276,11 +298,13 @@ and header assertions, as shown below:
276
298
.returnResult(MyEvent.class);
277
299
278
300
----
301
+ ====
279
302
280
303
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 :
283
306
307
+ ====
284
308
[source,java,intent=0]
285
309
[subs="verbatim,quotes"]
286
310
----
@@ -293,13 +317,15 @@ from the `reactor-test` module to do that, for example:
293
317
.thenCancel()
294
318
.verify();
295
319
----
320
+ ====
321
+
296
322
297
323
298
324
[[webtestclient-request-body]]
299
- === Request body
325
+ === Request Body
300
326
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,
305
331
and more.
0 commit comments