My problem is similar these:
BUT
Given a controller method signature:
public String setupForm(
@ModelAttribute("notification") Notification n, RedirectAttributes redirect)
And given a Test (using Mockito):
@Mock
private AddNotificationController handler;
@Test
public void get() throws Exception{
request.setRequestURI("/addNotification/save.do");
request.setMethod("GET");
adapter.handle(request, response, handler);
verify(handler,times(1)).setupForm(any(Notification.class), any(RedirectAttributes.class));
}
And when executing I got exception:
org.springframework.web.bind.annotation.support.HandlerMethodInvocationException:
Failed to invoke handler method [public java.lang.String ep.rdp.web.AddNotificationController.setupForm(a.b.c.Notification,org.springframework.web.servlet.mvc.support.RedirectAttributes)];
nested exception is java.lang.IllegalStateException:
Argument [RedirectAttributes] is of type Model or Map but is not assignable from the actual model.
You may need to switch newer MVC infrastructure classes to use this argument.
Actually I know the problem and know how to resolve by setting up spring properly but not in unit test.
So the question: what else I have to setup in mocks to make it working?
Additional info
Base setting up mocks:
@Before
public void setup() {
// adapter = new AnnotationMethodHandlerAdapter();
adapter = new RequestMappingHandlerAdapter();
request = new MockHttpServletRequest();
/*
* needed for AnnotationMethodHandlerAdapter when resolving controlle level mapping
*/
request.setAttribute(HandlerMapping.INTROSPECT_TYPE_LEVEL_MAPPING, Boolean.TRUE);
response = new MockHttpServletResponse();
}
In real
- when using AnnotationMethodHandlerAdapter i have this symptom.
when I use RequestMappingHandlerAdapter I got:
java.lang.ClassCastException: ep.rdp.web.CacheController$$EnhancerByMockitoWithCGLIB$$d8aab2c0 cannot be cast to org.springframework.web.method.HandlerMethod at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)