38
38
import org .springframework .web .context .WebApplicationContext ;
39
39
40
40
/**
41
- * {@link MockMvc} variant that tests Spring MVC exchanges and provides fluent
42
- * assertions using {@link org.assertj.core.api.Assertions AssertJ}.
41
+ * Test Spring MVC applications with {@link MockMvc} for server request handling
42
+ * using {@link org.assertj.core.api.Assertions AssertJ}.
43
+ *
44
+ * <p>A tester instance can be created from a {@link WebApplicationContext}:
45
+ * <pre><code class='java'>
46
+ * // Create an instance with default settings
47
+ * MockMvcTester mvc = MockMvcTester.from(applicationContext);
48
+ *
49
+ * // Create an instance with a custom Filter
50
+ * MockMvcTester mvc = MockMvcTester.from(applicationContext,
51
+ * builder -> builder.addFilters(filter).build());
52
+ * </code></pre>
53
+ *
54
+ * <p>A tester can be created standalone by providing the controller(s) to
55
+ * include in a standalone setup:<pre><code class='java'>
56
+ * // Create an instance for PersonController
57
+ * MockMvcTester mvc = MockMvcTester.of(new PersonController());
58
+ * </code></pre>
59
+ *
60
+ * <p>Once a test instance is available, you can perform requests in
61
+ * a similar fashion as with {@link MockMvc}, and wrapping the result in
62
+ * {@code assertThat} provides access to assertions. For instance:
63
+ * <pre><code class='java'>
64
+ * // perform a GET on /hi and assert the response body is equal to Hello
65
+ * assertThat(mvc.perform(get("/hi")))
66
+ * .hasStatusOk().hasBodyTextEqualTo("Hello");
67
+ * </code></pre>
43
68
*
44
69
* <p>A main difference with {@link MockMvc} is that an unresolved exception
45
- * is not thrown directly. Rather an {@link AssertableMvcResult} is available
46
- * with an {@link AssertableMvcResult#getUnresolvedException() unresolved
47
- * exception}.
70
+ * is not thrown directly. Rather an {@link MvcTestResult} is available
71
+ * with an {@link MvcTestResult#getUnresolvedException() unresolved
72
+ * exception}. You can assert that a request has failed unexpectedly:
73
+ * <pre><code class='java'>
74
+ * // perform a GET on /hi and assert the response body is equal to Hello
75
+ * assertThat(mvc.perform(get("/boom")))
76
+ * .hasUnresolvedException())
77
+ * .withMessage("Test exception");
78
+ * </code></pre>
48
79
*
49
- * <p>{@link AssertableMockMvc } can be configured with a list of
80
+ * <p>{@link MockMvcTester } can be configured with a list of
50
81
* {@linkplain HttpMessageConverter message converters} to allow the response
51
82
* body to be deserialized, rather than asserting on the raw values.
52
83
*
53
84
* @author Stephane Nicoll
54
85
* @author Brian Clozel
55
86
* @since 6.2
56
87
*/
57
- public final class AssertableMockMvc {
88
+ public final class MockMvcTester {
58
89
59
90
private static final MediaType JSON = MediaType .APPLICATION_JSON ;
60
91
@@ -64,23 +95,23 @@ public final class AssertableMockMvc {
64
95
private final GenericHttpMessageConverter <Object > jsonMessageConverter ;
65
96
66
97
67
- private AssertableMockMvc (MockMvc mockMvc , @ Nullable GenericHttpMessageConverter <Object > jsonMessageConverter ) {
98
+ private MockMvcTester (MockMvc mockMvc , @ Nullable GenericHttpMessageConverter <Object > jsonMessageConverter ) {
68
99
Assert .notNull (mockMvc , "mockMVC should not be null" );
69
100
this .mockMvc = mockMvc ;
70
101
this .jsonMessageConverter = jsonMessageConverter ;
71
102
}
72
103
73
104
/**
74
- * Create a {@link AssertableMockMvc } instance that delegates to the given
105
+ * Create a {@link MockMvcTester } instance that delegates to the given
75
106
* {@link MockMvc} instance.
76
107
* @param mockMvc the MockMvc instance to delegate calls to
77
108
*/
78
- public static AssertableMockMvc create (MockMvc mockMvc ) {
79
- return new AssertableMockMvc (mockMvc , null );
109
+ public static MockMvcTester create (MockMvc mockMvc ) {
110
+ return new MockMvcTester (mockMvc , null );
80
111
}
81
112
82
113
/**
83
- * Create an {@link AssertableMockMvc } instance using the given, fully
114
+ * Create an {@link MockMvcTester } instance using the given, fully
84
115
* initialized (i.e., <em>refreshed</em>) {@link WebApplicationContext}. The
85
116
* given {@code customizations} are applied to the {@link DefaultMockMvcBuilder}
86
117
* that ultimately creates the underlying {@link MockMvc} instance.
@@ -92,7 +123,7 @@ public static AssertableMockMvc create(MockMvc mockMvc) {
92
123
* instance based on a {@link DefaultMockMvcBuilder}.
93
124
* @see MockMvcBuilders#webAppContextSetup(WebApplicationContext)
94
125
*/
95
- public static AssertableMockMvc from (WebApplicationContext applicationContext ,
126
+ public static MockMvcTester from (WebApplicationContext applicationContext ,
96
127
Function <DefaultMockMvcBuilder , MockMvc > customizations ) {
97
128
98
129
DefaultMockMvcBuilder builder = MockMvcBuilders .webAppContextSetup (applicationContext );
@@ -101,7 +132,7 @@ public static AssertableMockMvc from(WebApplicationContext applicationContext,
101
132
}
102
133
103
134
/**
104
- * Shortcut to create an {@link AssertableMockMvc } instance using the given,
135
+ * Shortcut to create an {@link MockMvcTester } instance using the given,
105
136
* fully initialized (i.e., <em>refreshed</em>) {@link WebApplicationContext}.
106
137
* <p>Consider using {@link #from(WebApplicationContext, Function)} if
107
138
* further customization of the underlying {@link MockMvc} instance is
@@ -110,12 +141,12 @@ public static AssertableMockMvc from(WebApplicationContext applicationContext,
110
141
* MVC infrastructure and application controllers from
111
142
* @see MockMvcBuilders#webAppContextSetup(WebApplicationContext)
112
143
*/
113
- public static AssertableMockMvc from (WebApplicationContext applicationContext ) {
144
+ public static MockMvcTester from (WebApplicationContext applicationContext ) {
114
145
return from (applicationContext , DefaultMockMvcBuilder ::build );
115
146
}
116
147
117
148
/**
118
- * Create an {@link AssertableMockMvc } instance by registering one or more
149
+ * Create an {@link MockMvcTester } instance by registering one or more
119
150
* {@code @Controller} instances and configuring Spring MVC infrastructure
120
151
* programmatically.
121
152
* <p>This allows full control over the instantiation and initialization of
@@ -129,16 +160,16 @@ public static AssertableMockMvc from(WebApplicationContext applicationContext) {
129
160
* Spring MVC infrastructure
130
161
* @see MockMvcBuilders#standaloneSetup(Object...)
131
162
*/
132
- public static AssertableMockMvc of (Collection <?> controllers ,
163
+ public static MockMvcTester of (Collection <?> controllers ,
133
164
Function <StandaloneMockMvcBuilder , MockMvc > customizations ) {
134
165
135
166
StandaloneMockMvcBuilder builder = MockMvcBuilders .standaloneSetup (controllers .toArray ());
136
167
return create (customizations .apply (builder ));
137
168
}
138
169
139
170
/**
140
- * Shortcut to create an {@link AssertableMockMvc } instance by registering
141
- * one or more {@code @Controller} instances.
171
+ * Shortcut to create an {@link MockMvcTester } instance by registering one
172
+ * or more {@code @Controller} instances.
142
173
* <p>The minimum infrastructure required by the
143
174
* {@link org.springframework.web.servlet.DispatcherServlet DispatcherServlet}
144
175
* to serve requests with annotated controllers is created. Consider using
@@ -149,26 +180,26 @@ public static AssertableMockMvc of(Collection<?> controllers,
149
180
* into an instance
150
181
* @see MockMvcBuilders#standaloneSetup(Object...)
151
182
*/
152
- public static AssertableMockMvc of (Object ... controllers ) {
183
+ public static MockMvcTester of (Object ... controllers ) {
153
184
return of (Arrays .asList (controllers ), StandaloneMockMvcBuilder ::build );
154
185
}
155
186
156
187
/**
157
- * Return a new {@link AssertableMockMvc } instance using the specified
188
+ * Return a new {@link MockMvcTester } instance using the specified
158
189
* {@linkplain HttpMessageConverter message converters}.
159
190
* <p>If none are specified, only basic assertions on the response body can
160
191
* be performed. Consider registering a suitable JSON converter for asserting
161
192
* against JSON data structures.
162
193
* @param httpMessageConverters the message converters to use
163
194
* @return a new instance using the specified converters
164
195
*/
165
- public AssertableMockMvc withHttpMessageConverters (Iterable <HttpMessageConverter <?>> httpMessageConverters ) {
166
- return new AssertableMockMvc (this .mockMvc , findJsonMessageConverter (httpMessageConverters ));
196
+ public MockMvcTester withHttpMessageConverters (Iterable <HttpMessageConverter <?>> httpMessageConverters ) {
197
+ return new MockMvcTester (this .mockMvc , findJsonMessageConverter (httpMessageConverters ));
167
198
}
168
199
169
200
/**
170
- * Perform a request and return a type that can be used with standard
171
- * {@link org.assertj.core.api.Assertions AssertJ} assertions.
201
+ * Perform a request and return a {@link MvcTestResult result} that can be
202
+ * used with standard {@link org.assertj.core.api.Assertions AssertJ} assertions.
172
203
* <p>Use static methods of {@link MockMvcRequestBuilders} to prepare the
173
204
* request, wrapping the invocation in {@code assertThat}. The following
174
205
* asserts that a {@linkplain MockMvcRequestBuilders#get(URI) GET} request
@@ -191,16 +222,16 @@ public AssertableMockMvc withHttpMessageConverters(Iterable<HttpMessageConverter
191
222
* @param requestBuilder used to prepare the request to execute;
192
223
* see static factory methods in
193
224
* {@link org.springframework.test.web.servlet.request.MockMvcRequestBuilders}
194
- * @return an {@link AssertableMvcResult } to be wrapped in {@code assertThat}
225
+ * @return an {@link MvcTestResult } to be wrapped in {@code assertThat}
195
226
* @see MockMvc#perform(RequestBuilder)
196
227
*/
197
- public AssertableMvcResult perform (RequestBuilder requestBuilder ) {
228
+ public MvcTestResult perform (RequestBuilder requestBuilder ) {
198
229
Object result = getMvcResultOrFailure (requestBuilder );
199
230
if (result instanceof MvcResult mvcResult ) {
200
- return new DefaultAssertableMvcResult (mvcResult , null , this .jsonMessageConverter );
231
+ return new DefaultMvcTestResult (mvcResult , null , this .jsonMessageConverter );
201
232
}
202
233
else {
203
- return new DefaultAssertableMvcResult (null , (Exception ) result , this .jsonMessageConverter );
234
+ return new DefaultMvcTestResult (null , (Exception ) result , this .jsonMessageConverter );
204
235
}
205
236
}
206
237
0 commit comments