61
61
* builder -> builder.addFilters(filter).build());
62
62
* </code></pre>
63
63
*
64
- * <p>A tester can be created in standalone mode by providing the controller(s)
65
- * to include:<pre><code class="java">
64
+ * <p>A tester can be created in standalone mode by providing the controller
65
+ * instances to include:<pre><code class="java">
66
66
* // Create an instance for PersonController
67
67
* MockMvcTester mvc = MockMvcTester.of(new PersonController());
68
68
* </code></pre>
74
74
* assertThat(mvc.get().uri("/hi")).hasStatusOk().hasBodyTextEqualTo("Hello");
75
75
* </code></pre>
76
76
*
77
- *<p>For more complex scenarios the {@linkplain MvcTestResult result} of the
77
+ * <p>For more complex scenarios the {@linkplain MvcTestResult result} of the
78
78
* exchange can be assigned in a variable to run multiple assertions:
79
79
* <pre><code class="java">
80
80
* // perform a POST on /save and assert the response body is empty
83
83
* assertThat(result).body().isEmpty();
84
84
* </code></pre>
85
85
*
86
+ * <p>If the request is processing asynchronously, {@code exchange} waits for
87
+ * its completion, either using the
88
+ * {@linkplain org.springframework.mock.web.MockAsyncContext#setTimeout default
89
+ * timeout} or a given one. If you prefer to get the result of an
90
+ * asynchronous request immediately, use {@code asyncExchange}:
91
+ * <pre><code class="java">
92
+ * // perform a POST on /save and assert an asynchronous request has started
93
+ * assertThat(mvc.post().uri("/save").asyncExchange()).request().hasAsyncStarted();
94
+ * </code></pre>
95
+ *
86
96
* <p>You can also perform requests using the static builders approach that
87
97
* {@link MockMvc} uses. For instance:<pre><code class="java">
88
98
* // perform a GET on /hi and assert the response body is equal to Hello
89
99
* assertThat(mvc.perform(get("/hi")))
90
100
* .hasStatusOk().hasBodyTextEqualTo("Hello");
91
101
* </code></pre>
92
102
*
103
+ * <p>Use this approach if you have a custom {@link RequestBuilder} implementation
104
+ * that you'd like to integrate here. This approach is also invoking {@link MockMvc}
105
+ * without any additional processing of asynchronous requests.
106
+ *
93
107
* <p>One main difference between {@link MockMvc} and {@code MockMvcTester} is
94
108
* that an unresolved exception is not thrown directly when using
95
109
* {@code MockMvcTester}. Rather an {@link MvcTestResult} is available with an
@@ -231,8 +245,10 @@ public MockMvcTester withHttpMessageConverters(Iterable<HttpMessageConverter<?>>
231
245
* Prepare an HTTP GET request.
232
246
* <p>The returned builder can be wrapped in {@code assertThat} to enable
233
247
* assertions on the result. For multi-statements assertions, use
234
- * {@linkplain MockMvcRequestBuilder#exchange() exchange} to assign the
235
- * result.
248
+ * {@link MockMvcRequestBuilder#exchange() exchange()} to assign the
249
+ * result. To control the time to wait for asynchronous request to complete
250
+ * on a per-request basis, use
251
+ * {@link MockMvcRequestBuilder#exchange(Duration) exchange(Duration)}.
236
252
* @return a request builder for specifying the target URI
237
253
*/
238
254
public MockMvcRequestBuilder get () {
@@ -243,8 +259,10 @@ public MockMvcRequestBuilder get() {
243
259
* Prepare an HTTP HEAD request.
244
260
* <p>The returned builder can be wrapped in {@code assertThat} to enable
245
261
* assertions on the result. For multi-statements assertions, use
246
- * {@linkplain MockMvcRequestBuilder#exchange() exchange} to assign the
247
- * result.
262
+ * {@link MockMvcRequestBuilder#exchange() exchange()} to assign the
263
+ * result. To control the time to wait for asynchronous request to complete
264
+ * on a per-request basis, use
265
+ * {@link MockMvcRequestBuilder#exchange(Duration) exchange(Duration)}.
248
266
* @return a request builder for specifying the target URI
249
267
*/
250
268
public MockMvcRequestBuilder head () {
@@ -255,8 +273,10 @@ public MockMvcRequestBuilder head() {
255
273
* Prepare an HTTP POST request.
256
274
* <p>The returned builder can be wrapped in {@code assertThat} to enable
257
275
* assertions on the result. For multi-statements assertions, use
258
- * {@linkplain MockMvcRequestBuilder#exchange() exchange} to assign the
259
- * result.
276
+ * {@link MockMvcRequestBuilder#exchange() exchange()} to assign the
277
+ * result. To control the time to wait for asynchronous request to complete
278
+ * on a per-request basis, use
279
+ * {@link MockMvcRequestBuilder#exchange(Duration) exchange(Duration)}.
260
280
* @return a request builder for specifying the target URI
261
281
*/
262
282
public MockMvcRequestBuilder post () {
@@ -267,8 +287,10 @@ public MockMvcRequestBuilder post() {
267
287
* Prepare an HTTP PUT request.
268
288
* <p>The returned builder can be wrapped in {@code assertThat} to enable
269
289
* assertions on the result. For multi-statements assertions, use
270
- * {@linkplain MockMvcRequestBuilder#exchange() exchange} to assign the
271
- * result.
290
+ * {@link MockMvcRequestBuilder#exchange() exchange()} to assign the
291
+ * result. To control the time to wait for asynchronous request to complete
292
+ * on a per-request basis, use
293
+ * {@link MockMvcRequestBuilder#exchange(Duration) exchange(Duration)}.
272
294
* @return a request builder for specifying the target URI
273
295
*/
274
296
public MockMvcRequestBuilder put () {
@@ -279,8 +301,10 @@ public MockMvcRequestBuilder put() {
279
301
* Prepare an HTTP PATCH request.
280
302
* <p>The returned builder can be wrapped in {@code assertThat} to enable
281
303
* assertions on the result. For multi-statements assertions, use
282
- * {@linkplain MockMvcRequestBuilder#exchange() exchange} to assign the
283
- * result.
304
+ * {@link MockMvcRequestBuilder#exchange() exchange()} to assign the
305
+ * result. To control the time to wait for asynchronous request to complete
306
+ * on a per-request basis, use
307
+ * {@link MockMvcRequestBuilder#exchange(Duration) exchange(Duration)}.
284
308
* @return a request builder for specifying the target URI
285
309
*/
286
310
public MockMvcRequestBuilder patch () {
@@ -291,8 +315,10 @@ public MockMvcRequestBuilder patch() {
291
315
* Prepare an HTTP DELETE request.
292
316
* <p>The returned builder can be wrapped in {@code assertThat} to enable
293
317
* assertions on the result. For multi-statements assertions, use
294
- * {@linkplain MockMvcRequestBuilder#exchange() exchange} to assign the
295
- * result.
318
+ * {@link MockMvcRequestBuilder#exchange() exchange()} to assign the
319
+ * result. To control the time to wait for asynchronous request to complete
320
+ * on a per-request basis, use
321
+ * {@link MockMvcRequestBuilder#exchange(Duration) exchange(Duration)}.
296
322
* @return a request builder for specifying the target URI
297
323
*/
298
324
public MockMvcRequestBuilder delete () {
@@ -303,8 +329,10 @@ public MockMvcRequestBuilder delete() {
303
329
* Prepare an HTTP OPTIONS request.
304
330
* <p>The returned builder can be wrapped in {@code assertThat} to enable
305
331
* assertions on the result. For multi-statements assertions, use
306
- * {@linkplain MockMvcRequestBuilder#exchange() exchange} to assign the
307
- * result.
332
+ * {@link MockMvcRequestBuilder#exchange() exchange()} to assign the
333
+ * result. To control the time to wait for asynchronous request to complete
334
+ * on a per-request basis, use
335
+ * {@link MockMvcRequestBuilder#exchange(Duration) exchange(Duration)}.
308
336
* @return a request builder for specifying the target URI
309
337
*/
310
338
public MockMvcRequestBuilder options () {
@@ -315,44 +343,39 @@ public MockMvcRequestBuilder options() {
315
343
* Prepare a request for the specified {@code HttpMethod}.
316
344
* <p>The returned builder can be wrapped in {@code assertThat} to enable
317
345
* assertions on the result. For multi-statements assertions, use
318
- * {@linkplain MockMvcRequestBuilder#exchange() exchange} to assign the
319
- * result.
346
+ * {@link MockMvcRequestBuilder#exchange() exchange()} to assign the
347
+ * result. To control the time to wait for asynchronous request to complete
348
+ * on a per-request basis, use
349
+ * {@link MockMvcRequestBuilder#exchange(Duration) exchange(Duration)}.
320
350
* @return a request builder for specifying the target URI
321
351
*/
322
352
public MockMvcRequestBuilder method (HttpMethod method ) {
323
353
return new MockMvcRequestBuilder (method );
324
354
}
325
355
326
356
/**
327
- * Perform a request using {@link MockMvcRequestBuilders } and return a
357
+ * Perform a request using the given {@link RequestBuilder } and return a
328
358
* {@link MvcTestResult result} that can be used with standard
329
359
* {@link org.assertj.core.api.Assertions AssertJ} assertions.
330
- * <p>Use static methods of {@link MockMvcRequestBuilders} to prepare the
331
- * request, wrapping the invocation in {@code assertThat}. The following
332
- * asserts that a {@linkplain MockMvcRequestBuilders#get(URI) GET} request
333
- * against "/greet" has an HTTP status code 200 (OK) and a simple body:
334
- * <pre><code class="java">assertThat(mvc.perform(get("/greet")))
335
- * .hasStatusOk()
336
- * .body().asString().isEqualTo("Hello");
337
- * </code></pre>
360
+ * <p>Use only this method if you need to provide a custom
361
+ * {@link RequestBuilder}. For regular cases, users should initiate the
362
+ * configuration of the request using one of the methods available on
363
+ * this instance, e.g. {@link #get()} for HTTP GET.
338
364
* <p>Contrary to {@link MockMvc#perform(RequestBuilder)}, this does not
339
365
* throw an exception if the request fails with an unresolved exception.
340
366
* Rather, the result provides the exception, if any. Assuming that a
341
367
* {@link MockMvcRequestBuilders#post(URI) POST} request against
342
368
* {@code /boom} throws an {@code IllegalStateException}, the following
343
369
* asserts that the invocation has indeed failed with the expected error
344
370
* message:
345
- * <pre><code class="java">assertThat(mvc.perform( post("/boom")))
346
- * .unresolvedException ().isInstanceOf(IllegalStateException.class)
371
+ * <pre><code class="java">assertThat(mvc.post().uri ("/boom")))
372
+ * .failure ().isInstanceOf(IllegalStateException.class)
347
373
* .hasMessage("Expected");
348
374
* </code></pre>
349
- * @param requestBuilder used to prepare the request to execute;
350
- * see static factory methods in
351
- * {@link org.springframework.test.web.servlet.request.MockMvcRequestBuilders}
375
+ * @param requestBuilder used to prepare the request to execute
352
376
* @return an {@link MvcTestResult} to be wrapped in {@code assertThat}
353
377
* @see MockMvc#perform(RequestBuilder)
354
- * @see #get()
355
- * @see #post()
378
+ * @see #method(HttpMethod)
356
379
*/
357
380
public MvcTestResult perform (RequestBuilder requestBuilder ) {
358
381
Object result = getMvcResultOrFailure (requestBuilder );
0 commit comments