@@ -5079,20 +5079,43 @@ resulting links by using XPath expressions:
5079
5079
.andExpect(xpath("/person/ns:link[@rel='self']/@href", ns).string("http://localhost:8080/people"));
5080
5080
----
5081
5081
5082
- [[spring-mvc-test-server-filters]]
5083
- ===== Filter Registrations
5084
5082
5085
- When setting up a `MockMvc` instance, you can register one or more Servlet `Filter`
5086
- instances, as the following example shows:
5083
+ [[spring-mvc-test-async-requests]]
5084
+ ===== Async Requests
5085
+
5086
+ Servlet 3.0 asynchronous requests,
5087
+ <<web.adoc#mvc-ann-async,supported in Spring MVC>>, work by exiting the Servlet container
5088
+ thread and allowing the application to compute the response asynchronously, after which
5089
+ an async dispatch is made to complete processing on a Servlet container thread.
5090
+
5091
+ In Spring MVC Test, async requests can be tested by asserting the produced async value
5092
+ first, then manually performing the async dispatch, and finally verifying the response.
5093
+ Below is an example test for controller methods that return `DeferredResult`, `Callable`,
5094
+ or reactive type such as Reactor `Mono`:
5087
5095
5088
5096
[source,java,indent=0]
5089
5097
[subs="verbatim,quotes"]
5090
5098
----
5091
- mockMvc = standaloneSetup(new PersonController()).addFilters(new CharacterEncodingFilter()).build();
5099
+ @Test
5100
+ public void test() throws Exception {
5101
+ MvcResult mvcResult = this.mockMvc.perform(get("/path"))
5102
+ .andExpect(status().isOk()) <1>
5103
+ .andExpect(request().asyncStarted()) <2>
5104
+ .andExpect(request().asyncResult("body")) <3>
5105
+ .andReturn();
5106
+
5107
+ this.mockMvc.perform(asyncDispatch(mvcResult)) <4>
5108
+ .andExpect(status().isOk()) <5>
5109
+ .andExpect(content().string("body"));
5110
+ }
5092
5111
----
5112
+ <1> Check response status is still unchanged
5113
+ <2> Async processing must have started
5114
+ <3> Wait and assert the async result
5115
+ <4> Manually perform an ASYNC dispatch (as there is no running container)
5116
+ <5> Verify the final response
5093
5117
5094
- Registered filters are invoked through the `MockFilterChain` from `spring-test`, and the
5095
- last filter delegates to the `DispatcherServlet`.
5118
+ ====
5096
5119
5097
5120
5098
5121
[[spring-mvc-test-vs-streaming-response]]
@@ -5108,13 +5131,31 @@ with `WebTestClient`. One extra advantage is the ability to use the `StepVerifie
5108
5131
project Reactor that allows declaring expectations on a stream of data.
5109
5132
5110
5133
5134
+ [[spring-mvc-test-server-filters]]
5135
+ ===== Filter Registrations
5136
+
5137
+ When setting up a `MockMvc` instance, you can register one or more Servlet `Filter`
5138
+ instances, as the following example shows:
5139
+
5140
+ ====
5141
+ [source,java,indent=0]
5142
+ [subs="verbatim,quotes"]
5143
+ ----
5144
+ mockMvc = standaloneSetup(new PersonController()).addFilters(new CharacterEncodingFilter()).build();
5145
+ ----
5146
+ ====
5147
+
5148
+ Registered filters are invoked through the `MockFilterChain` from `spring-test`, and the
5149
+ last filter delegates to the `DispatcherServlet`.
5150
+
5151
+
5111
5152
[[spring-mvc-test-vs-end-to-end-integration-tests]]
5112
- ===== Differences Between Out-of-Container and End-to-End Integration Tests
5153
+ ===== Spring MVC Test vs End-to-End Tests
5113
5154
5114
- As mentioned earlier Spring MVC Test is built on the Servlet API mock objects from the
5115
- `spring-test` module and does not use a running Servlet container. Therefore, there are
5116
- some important differences compared to full end-to-end integration tests with an actual
5117
- client and server running.
5155
+ Spring MVC Test is built on Servlet API mock implementations from the
5156
+ `spring-test` module and does not rely on a running container. Therefore, there are
5157
+ some differences when compared to full end-to-end integration tests with an actual
5158
+ client and a live server running.
5118
5159
5119
5160
The easiest way to think about this is by starting with a blank `MockHttpServletRequest`.
5120
5161
Whatever you add to it is what the request becomes. Things that may catch you by surprise
@@ -5155,7 +5196,7 @@ important thing to check. In short, there is room here for multiple styles and s
5155
5196
of testing even within the same project.
5156
5197
5157
5198
[[spring-mvc-test-server-resources]]
5158
- ===== Further Server-Side Test Examples
5199
+ ===== Further Examples
5159
5200
5160
5201
The framework's own tests include
5161
5202
https://github.com/spring-projects/spring-framework/tree/master/spring-test/src/test/java/org/springframework/test/web/servlet/samples[many
0 commit comments