|
1 | 1 | /* |
2 | | - * Copyright 2002-2015 the original author or authors. |
| 2 | + * Copyright 2002-2016 the original author or authors. |
3 | 3 | * |
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | 5 | * you may not use this file except in compliance with the License. |
|
27 | 27 | import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter; |
28 | 28 | import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping; |
29 | 29 |
|
30 | | -import static org.hamcrest.MatcherAssert.*; |
31 | | -import static org.springframework.test.util.AssertionErrors.*; |
| 30 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 31 | +import static org.springframework.test.util.AssertionErrors.assertEquals; |
| 32 | +import static org.springframework.test.util.AssertionErrors.assertTrue; |
32 | 33 |
|
33 | 34 | /** |
34 | | - * Factory for assertions on the selected handler. |
| 35 | + * Factory for assertions on the selected handler or handler method. |
35 | 36 | * <p>An instance of this class is typically accessed via |
36 | 37 | * {@link MockMvcResultMatchers#handler}. |
37 | 38 | * |
| 39 | + * <p><strong>Note:</strong> Expectations that assert the controller method |
| 40 | + * used to process the request work only for requests processed with |
| 41 | + * {@link RequestMappingHandlerMapping} and {@link RequestMappingHandlerAdapter} |
| 42 | + * which is used by default with the Spring MVC Java config and XML namespace. |
| 43 | + * |
38 | 44 | * @author Rossen Stoyanchev |
39 | 45 | * @since 3.2 |
40 | 46 | */ |
41 | 47 | public class HandlerResultMatchers { |
42 | 48 |
|
| 49 | + |
43 | 50 | /** |
44 | 51 | * Protected constructor. |
45 | 52 | * Use {@link MockMvcResultMatchers#handler()}. |
@@ -67,56 +74,50 @@ public void match(MvcResult result) throws Exception { |
67 | 74 | } |
68 | 75 |
|
69 | 76 | /** |
70 | | - * Assert the name of the controller method that processed the request with |
71 | | - * the given Hamcrest {@link Matcher}. |
72 | | - * <p>Use of this method implies annotated controllers are processed with |
73 | | - * {@link RequestMappingHandlerMapping} and {@link RequestMappingHandlerAdapter}. |
| 77 | + * Assert the name of the controller method used to process the request |
| 78 | + * using the given Hamcrest {@link Matcher}. |
74 | 79 | */ |
75 | 80 | public ResultMatcher methodName(final Matcher<? super String> matcher) { |
76 | 81 | return new ResultMatcher() { |
77 | 82 | @Override |
78 | 83 | public void match(MvcResult result) throws Exception { |
79 | | - Object handler = assertHandlerMethod(result); |
80 | | - assertThat("HandlerMethod", ((HandlerMethod) handler).getMethod().getName(), matcher); |
| 84 | + HandlerMethod handlerMethod = getHandlerMethod(result); |
| 85 | + assertThat("HandlerMethod", handlerMethod.getMethod().getName(), matcher); |
81 | 86 | } |
82 | 87 | }; |
83 | 88 | } |
84 | 89 |
|
85 | 90 | /** |
86 | | - * Assert the name of the controller method that processed the request. |
87 | | - * <p>Use of this method implies annotated controllers are processed with |
88 | | - * {@link RequestMappingHandlerMapping} and {@link RequestMappingHandlerAdapter}. |
| 91 | + * Assert the name of the controller method used to process the request. |
89 | 92 | */ |
90 | 93 | public ResultMatcher methodName(final String name) { |
91 | 94 | return new ResultMatcher() { |
92 | 95 | @Override |
93 | 96 | public void match(MvcResult result) throws Exception { |
94 | | - Object handler = assertHandlerMethod(result); |
95 | | - assertEquals("HandlerMethod", name, ((HandlerMethod) handler).getMethod().getName()); |
| 97 | + HandlerMethod handlerMethod = getHandlerMethod(result); |
| 98 | + assertEquals("HandlerMethod", name, handlerMethod.getMethod().getName()); |
96 | 99 | } |
97 | 100 | }; |
98 | 101 | } |
99 | 102 |
|
100 | 103 | /** |
101 | | - * Assert the controller method that processed the request. |
102 | | - * <p>Use of this method implies annotated controllers are processed with |
103 | | - * {@link RequestMappingHandlerMapping} and {@link RequestMappingHandlerAdapter}. |
| 104 | + * Assert the controller method used to process the request. |
104 | 105 | */ |
105 | 106 | public ResultMatcher method(final Method method) { |
106 | 107 | return new ResultMatcher() { |
107 | 108 | @Override |
108 | 109 | public void match(MvcResult result) throws Exception { |
109 | | - Object handler = assertHandlerMethod(result); |
110 | | - assertEquals("HandlerMethod", method, ((HandlerMethod) handler).getMethod()); |
| 110 | + HandlerMethod handlerMethod = getHandlerMethod(result); |
| 111 | + assertEquals("HandlerMethod", method, handlerMethod.getMethod()); |
111 | 112 | } |
112 | 113 | }; |
113 | 114 | } |
114 | 115 |
|
115 | | - private static Object assertHandlerMethod(MvcResult result) { |
| 116 | + private static HandlerMethod getHandlerMethod(MvcResult result) { |
116 | 117 | Object handler = result.getHandler(); |
117 | 118 | assertTrue("No handler: ", handler != null); |
118 | 119 | assertTrue("Not a HandlerMethod: " + handler, HandlerMethod.class.isInstance(handler)); |
119 | | - return handler; |
| 120 | + return (HandlerMethod) handler; |
120 | 121 | } |
121 | 122 |
|
122 | 123 | } |
0 commit comments