0

I am getting this Exception while upgrading from Spring 3.0 to 4.0

I updated the deprecated classes with this link

DefaultAnnotationHandlerMapping,AnnotationMethodHandlerAdapter with RequestMappingHandlerMapping ,RequestMappingHandlerAdapter

java.lang.ClassCastException: AuditingController$$EnhancerByCGLIB$$992fb2c8 cannot be cast to org.springframework.web.method.HandlerMethod 

at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)

Here is beans definition

<bean name="handlerAdapter"
    class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
            <property name="messageConverters">
        <array>
            ....

           ....
        </array>
    </property>
</bean>

In my testcase calling this function.

  handlerAdapter.handle(request, response, controller);

which calls org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter

return handleInternal(request, response, (HandlerMethod) handler);

here it fails to cast the controller to HandlerMethod

Gangaraju
  • 4,406
  • 9
  • 45
  • 77
  • Your testcase is wrong, handling `@RequestMethod` annotated methods has changed considerable from `AnnotationMethodHandlerAdapter` to `RequestMappingHandlerAdapter`. It now points directly to the method to invoke (i.e. the `HandlerMethod` ) so putting your full controller in there isn't going to work anymore, – M. Deinum Jan 30 '14 at 14:50
  • Why are you even testing Spring classes? You should be testing your `@Controller` beans. These `HandlerAdapter` objects are used by Spring's `DispatcherServlet` stack (and MVC configuration). You should assume they work and test your own code. – Sotirios Delimanolis Jan 30 '14 at 14:51
  • @SotiriosDelimanolis - I am testing my controller itself. Not testing spring classes. I am using `MockHttpServletRequest, MockHttpServletResponse, HandlerAdapter` and testing the response data – Gangaraju Jan 30 '14 at 15:08
  • What I'm trying to say is that you shouldn't. The whole point of using Spring MVC is to go through the `DispatcherServlet`. You're not really testing your controllers if you are going around the `DispatcherServlet`. Just set up a `MockMvc` object from your MVC configuration and test your requests through there. – Sotirios Delimanolis Jan 30 '14 at 15:09

1 Answers1

0

Its working now. As mentioned in this link, I need to update my controller test cases to use

WebApplicationContext,MockMvc & RequestMappingHandlerAdapter 

instead of

MockHttpServletRequest, MockHttpServletResponse & HandlerAdapter.
Gangaraju
  • 4,406
  • 9
  • 45
  • 77