`
sillycat
  • 浏览: 2487447 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

Spring3 and REST Integration(V)Controller JUnit Test and Mock/HandlerAdapter

 
阅读更多
Spring3 and REST Integration(V)Controller JUnit Test and Mock/HandlerAdapter
HandlerAdapter with Settle Controller
We use handlerAdapter to execute our controller, and use handlerMapping to find our URI related controller class. So that is the problem, we can not mock the service class or manager class in controller.

The base test class is needed as follow:
package com.sillycat.easyrestserver.core;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.junit.BeforeClass;
import org.springframework.mock.web.MockServletContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.XmlWebApplicationContext;
import org.springframework.web.servlet.HandlerAdapter;
import org.springframework.web.servlet.HandlerExecutionChain;
import org.springframework.web.servlet.HandlerMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter;
import org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping;

public class ControllerTestBase {

     private static HandlerMapping handlerMapping;
     private static HandlerAdapter handlerAdapter;

     @BeforeClass
     public static void setUp() {
          if (handlerMapping == null) {
               String[] configs = { "file:src/test/resources/test-context.xml" };
               XmlWebApplicationContext context = new XmlWebApplicationContext();
               context.setConfigLocations(configs);
               MockServletContext msc = new MockServletContext();
               context.setServletContext(msc);
               context.refresh();
               msc.setAttribute(
                         WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE,
                         context);
               handlerMapping = (HandlerMapping) context
                         .getBean(DefaultAnnotationHandlerMapping.class);
               handlerAdapter = (HandlerAdapter) context
                         .getBean(context
                                   .getBeanNamesForType(AnnotationMethodHandlerAdapter.class)[0]);
          }
     }

     public ModelAndView excuteAction(HttpServletRequest request,
               HttpServletResponse response) throws Exception {
          HandlerExecutionChain chain = handlerMapping.getHandler(request);
          //deal with the error message
          request.setAttribute(HandlerMapping.INTROSPECT_TYPE_LEVEL_MAPPING, true);

          final ModelAndView model = handlerAdapter.handle(request, response,
                    chain.getHandler());
          return model;
     }

}


The test class will be as follow:
package com.sillycat.easyrestserver.controller;

import org.junit.Test;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;

import com.sillycat.easyrestserver.core.ControllerTestBase;

/**
* handlerAdapter and handlerMapping to verify our controller
* can not mock the service or manager in controller
* @author karl
*
*/
public class PersonControllerTest2 extends ControllerTestBase{

     @Test
    public void testAdd() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest();
        MockHttpServletResponse response = new MockHttpServletResponse();
        request.setRequestURI("/person/1");
        request.setMethod("GET");
        this.excuteAction(request, response);
    }

}


But once I run this project, I got the error message as follow:
error message:
java.lang.NullPointerException
     at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter$ServletHandlerMethodResolver.useTypeLevelMapping(AnnotationMethodHandlerAdapter.java:675)
     at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter$ServletHandlerMethodResolver.getCombinedPattern(AnnotationMethodHandlerAdapter.java:690)
     at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter$ServletHandlerMethodResolver.resolveHandlerMethod(AnnotationMethodHandlerAdapter.java:569)
     at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:431)

     at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:424)

solution:
I go over the spring source code, I really can not understand why the spring framework 3.1.1 exist this kind of problem. But I try this solution:
//deal with the error message
          request.setAttribute(HandlerMapping.INTROSPECT_TYPE_LEVEL_MAPPING, true);
set the request to true in my base class can solve this problem.

references:


分享到:
评论
1 楼 containsoft 2013-03-04  
very good.

相关推荐

Global site tag (gtag.js) - Google Analytics