37
37
import org .springframework .web .context .request .RequestAttributes ;
38
38
import org .springframework .web .context .request .RequestContextHolder ;
39
39
import org .springframework .web .context .request .ServletRequestAttributes ;
40
- import org .springframework .web .context .support .WebApplicationContextUtils ;
41
40
import org .springframework .web .method .annotation .RequestParamMethodArgumentResolver ;
42
41
import org .springframework .web .method .support .CompositeUriComponentsContributor ;
43
42
import org .springframework .web .servlet .DispatcherServlet ;
44
- import org .springframework .web .servlet .support .RequestContextUtils ;
45
43
import org .springframework .web .servlet .support .ServletUriComponentsBuilder ;
46
44
import org .springframework .web .util .UriComponents ;
47
45
import org .springframework .web .util .UriComponentsBuilder ;
48
46
49
- import javax .servlet .ServletContext ;
50
47
import javax .servlet .http .HttpServletRequest ;
51
48
import java .lang .reflect .Method ;
52
49
import java .util .*;
@@ -196,15 +193,15 @@ public static UriComponentsBuilder fromMethod(Method method, Object... argumentV
196
193
*
197
194
* // Inline style with static import of MvcUriComponentsBuilder.mock
198
195
*
199
- * MvcUriComponentsBuilder.fromLastCall (
196
+ * MvcUriComponentsBuilder.fromMethodCall (
200
197
* mock(CustomerController.class).showAddresses("US")).buildAndExpand(1);
201
198
*
202
199
* // Longer style for preparing multiple URIs and for void controller methods
203
200
*
204
201
* CustomerController controller = MvcUriComponentsBuilder.mock(CustomController.class);
205
202
* controller.addAddress(null);
206
203
*
207
- * MvcUriComponentsBuilder.fromLastCall (controller);
204
+ * MvcUriComponentsBuilder.fromMethodCall (controller);
208
205
*
209
206
* </pre>
210
207
*
@@ -219,7 +216,7 @@ public static UriComponentsBuilder fromMethod(Method method, Object... argumentV
219
216
*
220
217
* @return a UriComponents instance
221
218
*/
222
- public static UriComponentsBuilder fromLastCall (Object methodInvocationInfo ) {
219
+ public static UriComponentsBuilder fromMethodCall (Object methodInvocationInfo ) {
223
220
224
221
Assert .isInstanceOf (MethodInvocationInfo .class , methodInvocationInfo );
225
222
MethodInvocationInfo info = (MethodInvocationInfo ) methodInvocationInfo ;
@@ -319,39 +316,47 @@ protected static CompositeUriComponentsContributor getConfiguredUriComponentsCon
319
316
}
320
317
321
318
/**
322
- * Return a "mock" controller instance. When a method on the mock is invoked, the
323
- * supplied argument values are remembered and the result can then be used to
324
- * prepare a UriComponents through the factory method {@link #fromLastCall(Object)}.
319
+ * Return a "mock" controller instance. When an {@code @RequestMapping} method
320
+ * on the controller is invoked, the supplied argument values are remembered
321
+ * and the result can then be used to prepare a URL to the method via
322
+ * {@link #fromMethodCall(Object)}.
325
323
* <p>
326
- * The controller can be invoked more than once. However, only the last invocation
327
- * is remembered. This means the same mock controller can be used to create
328
- * multiple links in succession, for example:
324
+ * This is a shorthand version of {@link #controller(Class)} intended for
325
+ * inline use as follows:
329
326
*
330
327
* <pre>
331
- * CustomerController controller = MvcUriComponentsBuilder.mock(CustomController.class);
332
- *
333
- * MvcUriComponentsBuilder.fromLastCall(controller.getFoo(1)).build();
334
- * MvcUriComponentsBuilder.fromLastCall(controller.getFoo(2)).build();
335
- *
336
- * MvcUriComponentsBuilder.fromLastCall(controller.getBar(1)).build();
337
- * MvcUriComponentsBuilder.fromLastCall(controller.getBar(2)).build();
328
+ * UriComponentsBuilder builder = MvcUriComponentsBuilder.fromMethodCall(
329
+ * on(FooController.class).getFoo(1)).build();
338
330
* </pre>
339
331
*
340
- * If a controller method returns void, use the following style:
332
+ * @param controllerType the target controller
333
+ */
334
+ public static <T > T on (Class <T > controllerType ) {
335
+ return controller (controllerType );
336
+ }
337
+
338
+ /**
339
+ * Return a "mock" controller instance. When an {@code @RequestMapping} method
340
+ * on the controller is invoked, the supplied argument values are remembered
341
+ * and the result can then be used to prepare a URL to the method via
342
+ * {@link #fromMethodCall(Object)}.
343
+ * <p>
344
+ * This is a longer version of {@link #on(Class)} for use with void controller
345
+ * methods as well as for creating multiple links in succession.
341
346
*
342
347
* <pre>
343
- * CustomerController controller = MvcUriComponentsBuilder.mock(CustomController .class);
348
+ * FooController fooController = controller(FooController .class);
344
349
*
345
- * controller.handleFoo(1 );
346
- * MvcUriComponentsBuilder.fromLastCall(controller).build( );
350
+ * fooController.saveFoo(1, null );
351
+ * builder = MvcUriComponentsBuilder.fromMethodCall(fooController );
347
352
*
348
- * controller.handleFoo(2)
349
- * MvcUriComponentsBuilder.fromLastCall(controller).build( );
353
+ * fooController.saveFoo(2, null);
354
+ * builder = MvcUriComponentsBuilder.fromMethodCall(fooController );
350
355
* </pre>
351
356
*
352
- * @param controllerType the type of controller to mock
357
+ * @param controllerType the target controller
353
358
*/
354
- public static <T > T mock (Class <T > controllerType ) {
359
+ public static <T > T controller (Class <T > controllerType ) {
355
360
Assert .notNull (controllerType , "'controllerType' must not be null" );
356
361
return initProxy (controllerType , new ControllerMethodInvocationInterceptor ());
357
362
}
0 commit comments