Announcement Announcement Module
No announcement yet.
Spring 3.1.2 - @Controller annotated Controllers JUnit test fail Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring 3.1.2 - @Controller annotated Controllers JUnit test fail

    Hi everybody, I have just ran into a really strange problem and it is driving me crazy. I've got a starting project whom has a spring library included (Intelli J Idea downloaded them). But I needed a custom HTTPMessage converter so I Downloaded the latest release from the website and updated my project libraries.

    After the update my Controller tests started to fail after each other. The reason is the following exception:
    org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [org.springframework.web.servlet.HandlerAdapter] is defined: expected single bean but found 3: org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter,org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter,org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter#0
    The way I was tested my controllers is the following (note: this code is not working after the update):
    import junit.framework.TestCase;
    import org.junit.Before;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.ApplicationContext;
    import org.springframework.mock.web.MockHttpServletRequest;
    import org.springframework.mock.web.MockHttpServletResponse;
    import org.springframework.test.context.ContextConfiguration;
    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    import org.springframework.web.servlet.HandlerAdapter;
    import org.springframework.web.servlet.ModelAndView;
    import static org.springframework.test.web.ModelAndViewAssert.assertViewName;
     * <p>HomeController test</p>
    public class HomeControllerTest extends TestCase {
        private ApplicationContext applicationContext;
        private MockHttpServletRequest request;
        private MockHttpServletResponse response;
        private HandlerAdapter handlerAdapter;
        private HomeController controller;
        public void setUp() {
            request = new MockHttpServletRequest();
            response = new MockHttpServletResponse();
            handlerAdapter = applicationContext.getBean(HandlerAdapter.class);
            controller = new HomeController();
        public void testView() throws Exception {
            final ModelAndView mav = handlerAdapter.handle(request, response, controller);
            assertViewName(mav, "home");
    and so on.

    I tried to get an instance of HTTPRequestHandlerAdapter but its handle method needs type of HandlerMethod so I got a ClassCastException because the instance of the Controller is not a handler method.

    So my question is: is there anything changed since last release or I am doing something wrong? Any help would be appreciated.

    Thank you so much.