|
| 1 | +[[mockmvc-tester-requests]] |
| 2 | += Performing Requests |
| 3 | + |
| 4 | +This section shows how to use `MockMvcTester` to perform requests and its integration |
| 5 | +with AssertJ to verify responses. |
| 6 | + |
| 7 | +`MockMvcTester` provides a fluent API to compose the request that reuses the same |
| 8 | +`MockHttpServletRequestBuilder` as the Hamcrest support, except that there is no need |
| 9 | +to import a static method. The builder that is returned is AssertJ-aware so that |
| 10 | +wrapping it in the regular `assertThat()` factory method triggers the exchange and |
| 11 | +provides access to a dedicated Assert object for `MvcTestResult`. |
| 12 | + |
| 13 | +Here is a simple example that performs a `POST` on `/hotels/42` and configures the |
| 14 | +request to specify an `Accept` header: |
| 15 | + |
| 16 | +include-code::./HotelControllerTests[tag=post,indent=0] |
| 17 | + |
| 18 | +AssertJ often consists of multiple `assertThat()` statements to validate the different |
| 19 | +parts of the exchange. Rather than having a single statement as in the case above, you |
| 20 | +can use `.exchange()` to return a `MvcTestResult` that can be used in multiple |
| 21 | +`assertThat` statements: |
| 22 | + |
| 23 | +include-code::./HotelControllerTests[tag=post-exchange,indent=0] |
| 24 | + |
| 25 | +You can specify query parameters in URI template style, as the following example shows: |
| 26 | + |
| 27 | +include-code::./HotelControllerTests[tag=query-parameters,indent=0] |
| 28 | + |
| 29 | +You can also add Servlet request parameters that represent either query or form |
| 30 | +parameters, as the following example shows: |
| 31 | + |
| 32 | +include-code::./HotelControllerTests[tag=parameters,indent=0] |
| 33 | + |
| 34 | +If application code relies on Servlet request parameters and does not check the query |
| 35 | +string explicitly (as is most often the case), it does not matter which option you use. |
| 36 | +Keep in mind, however, that query parameters provided with the URI template are decoded |
| 37 | +while request parameters provided through the `param(...)` method are expected to already |
| 38 | +be decoded. |
| 39 | + |
| 40 | + |
| 41 | +[[mockmvc-tester-requests-async]] |
| 42 | +== Async |
| 43 | + |
| 44 | +If the processing of the request is done asynchronously, `exchange()` waits for |
| 45 | +the completion of the request so that the result to assert is effectively immutable. |
| 46 | +The default timeout is 10 seconds but it can be controlled on a request-by-request |
| 47 | +basis as shown in the following example: |
| 48 | + |
| 49 | +include-code::./AsyncControllerTests[tag=duration,indent=0] |
| 50 | + |
| 51 | +If you prefer to get the raw result and manage the lifecycle of the asynchronous |
| 52 | +request yourself, use `asyncExchange` rather than `exchange`. |
| 53 | + |
| 54 | +[[mockmvc-tester-requests-multipart]] |
| 55 | +== Multipart |
| 56 | + |
| 57 | +You can perform file upload requests that internally use |
| 58 | +`MockMultipartHttpServletRequest` so that there is no actual parsing of a multipart |
| 59 | +request. Rather, you have to set it up to be similar to the following example: |
| 60 | + |
| 61 | +include-code::./MultipartControllerTests[tag=snippet,indent=0] |
| 62 | + |
| 63 | +[[mockmvc-tester-requests-paths]] |
| 64 | +== Using Servlet and Context Paths |
| 65 | + |
| 66 | +In most cases, it is preferable to leave the context path and the Servlet path out of the |
| 67 | +request URI. If you must test with the full request URI, be sure to set the `contextPath` |
| 68 | +and `servletPath` accordingly so that request mappings work, as the following example |
| 69 | +shows: |
| 70 | + |
| 71 | +include-code::./HotelControllerTests[tag=context-servlet-paths,indent=0] |
| 72 | + |
| 73 | +In the preceding example, it would be cumbersome to set the `contextPath` and |
| 74 | +`servletPath` with every performed request. Instead, you can set up default request |
| 75 | +properties, as the following example shows: |
| 76 | + |
| 77 | +include-code::./HotelControllerTests[tag=default-customizations,indent=0] |
| 78 | + |
| 79 | +The preceding properties affect every request performed through the `mockMvc` instance. |
| 80 | +If the same property is also specified on a given request, it overrides the default |
| 81 | +value. That is why the HTTP method and URI in the default request do not matter, since |
| 82 | +they must be specified on every request. |
| 83 | + |
0 commit comments