`
sillycat
  • 浏览: 2566438 次
  • 性别: 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.

相关推荐

    pimpinella_3cd_01_0716.pdf

    pimpinella_3cd_01_0716

    FIB English learning

    FIB English learning

    linux下 jq 截取json文件信息

    X86-jq安装包

    [AB PLC例程源码][MMS_046356]SELX.zip

    AB PLC例程代码项目案例 【备注】 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用!有问题请及时沟通交流。 2、适用人群:计算机相关专业(如计科、信息安全、数据科学与大数据技术、人工智能、通信、物联网、自动化、电子信息等)在校学生、专业老师或者企业员工下载使用。 3、用途:项目具有较高的学习借鉴价值,不仅适用于小白学习入门进阶。也可作为毕设项目、课程设计、大作业、初期项目立项演示等。 4、如果基础还行,或热爱钻研,亦可在此项目代码基础上进行修改添加,实现其他不同功能。 欢迎下载!欢迎交流学习!不清楚的可以私信问我!

    大圣挪车小程序1.3.5+前端.zip

    大圣挪车小程序1.3.5 前端

    Manus.im 产品及开发团队研究报告.pdf

    Manus.im 产品及开发团队研究报告.pdf

    [AB PLC例程源码][MMS_044663]Control daisy chain wiring in Fieldbus Foundation.zip

    AB PLC例程代码项目案例 【备注】 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用!有问题请及时沟通交流。 2、适用人群:计算机相关专业(如计科、信息安全、数据科学与大数据技术、人工智能、通信、物联网、自动化、电子信息等)在校学生、专业老师或者企业员工下载使用。 3、用途:项目具有较高的学习借鉴价值,不仅适用于小白学习入门进阶。也可作为毕设项目、课程设计、大作业、初期项目立项演示等。 4、如果基础还行,或热爱钻研,亦可在此项目代码基础上进行修改添加,实现其他不同功能。 欢迎下载!欢迎交流学习!不清楚的可以私信问我!

    sun_3ck_01a_0918.pdf

    sun_3ck_01a_0918

    支持适用于PERC H330/H730/H730P/H830/H730P系列RAID卡MX/FD33xD/FD33xS控制器的驱动安装指南

    下载 1. 单击“立即下载”,以下载该文件。 2. 出现“文件下载”窗口后,单击“保存”,以将文件保存到硬盘。 安装 1. 浏览至文件下载目标位置并双击新下载的文件。 2. 仔细阅读对话窗口中显示的发布信息。 3. 下载并安装对话窗口中标识的任何必备项,然后再继续。 4. 单击“Install”(安装)按钮。 5. 按照其余提示执行更新。 安装 1. 将解压的文件复制到可访问Windows的介质。 2. 将系统重新引导至Windows操作系统。 3. 打开“服务器管理器”->“设备管理器”->“存储控制器”,然后单击“PERC控制器”。 5. 单击“更新驱动程序软件”,并按照提示更新驱动程序。 4. 重新引导系统以使更改生效。

    硬盘安装器,支持硬盘安装,无需制作U盘PE!

    支持所有操作系统一键安装。

    matlab程序代码项目案例:使用 Simulink 进行自适应 MPC 设计

    matlab程序代码项目案例 【备注】 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用!有问题请及时沟通交流。 2、适用人群:计算机相关专业(如计科、信息安全、数据科学与大数据技术、人工智能、通信、物联网、自动化、电子信息等)在校学生、专业老师或者企业员工下载使用。 3、用途:项目具有较高的学习借鉴价值,不仅适用于小白学习入门进阶。也可作为毕设项目、课程设计、大作业、初期项目立项演示等。 4、如果基础还行,或热爱钻研,亦可在此项目代码基础上进行修改添加,实现其他不同功能。 欢迎下载!欢迎交流学习!不清楚的可以私信问我!

    [AB PLC例程源码][MMS_044098]1769-ASCII Simultaneous Mode.zip

    AB PLC例程代码项目案例 【备注】 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用!有问题请及时沟通交流。 2、适用人群:计算机相关专业(如计科、信息安全、数据科学与大数据技术、人工智能、通信、物联网、自动化、电子信息等)在校学生、专业老师或者企业员工下载使用。 3、用途:项目具有较高的学习借鉴价值,不仅适用于小白学习入门进阶。也可作为毕设项目、课程设计、大作业、初期项目立项演示等。 4、如果基础还行,或热爱钻研,亦可在此项目代码基础上进行修改添加,实现其他不同功能。 欢迎下载!欢迎交流学习!不清楚的可以私信问我!

    swanson_01_1106.pdf

    swanson_01_1106

    [AB PLC例程源码][MMS_047811]SAF1 - Store.zip

    AB PLC例程代码项目案例 【备注】 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用!有问题请及时沟通交流。 2、适用人群:计算机相关专业(如计科、信息安全、数据科学与大数据技术、人工智能、通信、物联网、自动化、电子信息等)在校学生、专业老师或者企业员工下载使用。 3、用途:项目具有较高的学习借鉴价值,不仅适用于小白学习入门进阶。也可作为毕设项目、课程设计、大作业、初期项目立项演示等。 4、如果基础还行,或热爱钻研,亦可在此项目代码基础上进行修改添加,实现其他不同功能。 欢迎下载!欢迎交流学习!不清楚的可以私信问我!

    [AB PLC例程源码][MMS_043879]Programming in SFC and ST Language.zip

    AB PLC例程代码项目案例 【备注】 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用!有问题请及时沟通交流。 2、适用人群:计算机相关专业(如计科、信息安全、数据科学与大数据技术、人工智能、通信、物联网、自动化、电子信息等)在校学生、专业老师或者企业员工下载使用。 3、用途:项目具有较高的学习借鉴价值,不仅适用于小白学习入门进阶。也可作为毕设项目、课程设计、大作业、初期项目立项演示等。 4、如果基础还行,或热爱钻研,亦可在此项目代码基础上进行修改添加,实现其他不同功能。 欢迎下载!欢迎交流学习!不清楚的可以私信问我!

    sun_3ck_01_0919.pdf

    sun_3ck_01_0919

    方言距离数据.岭南学院产业与区域经济研究中心

    各城市方言距离数据-中山大学岭南学院产业与区域经济研究中心 方言距离是指两种或多种方言之间的相似程度或差异程度。参考中山大学岭南学院产业与区域经济研究中心的刘毓芸等(2015)文献。他们基于方言树图,并参考《汉语方言大词典》和《中国语言地图集》对方言的划分,将汉语方言从宽泛到具体分为以下几个层级:汉语→方言大区→方言区→方言片。为了量化县与县之间的方言差异,他们采用了一种赋值方法: 若它们分属不同方言大区,则距离为3。: 若两个县同属一个方言片,则它们之间的方言距离为0; 若两个县属于同一方言区但不同方言片,则距离为1; 若它们属于同一方言大区但不同方言区,则距离为2; 方言距离是一个反映方言之间相似程度或差异程度的重要指标,它在语音识别、方言研究等领域具有广泛的应用价值。 参考文献:[1]刘毓芸, 徐现祥, 肖泽凯. 2015. 劳动力跨方言流动的倒U型模式[J]. 经济研究, 50(10): 134-146+162. 指标 语系、语族、方言大区、方言区/语支、方言片/语种、Supergroup、Dialect、group、Sub-dialect、groupPref_1、Pref_2、DiaDist、PrefCode_1、PrefCode_2等等。

    基于PCA算法的人脸识别MATLAB源码

    基于PCA算法的人脸识别MATLAB源码

    [AB PLC例程源码][MMS_045740]Handling manual movement of axis using an Add On Instruction (AOI), .zip

    AB PLC例程代码项目案例 【备注】 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用!有问题请及时沟通交流。 2、适用人群:计算机相关专业(如计科、信息安全、数据科学与大数据技术、人工智能、通信、物联网、自动化、电子信息等)在校学生、专业老师或者企业员工下载使用。 3、用途:项目具有较高的学习借鉴价值,不仅适用于小白学习入门进阶。也可作为毕设项目、课程设计、大作业、初期项目立项演示等。 4、如果基础还行,或热爱钻研,亦可在此项目代码基础上进行修改添加,实现其他不同功能。 欢迎下载!欢迎交流学习!不清楚的可以私信问我!

    lim_3ck_01a_0518.pdf

    lim_3ck_01a_0518

Global site tag (gtag.js) - Google Analytics