zoukankan      html  css  js  c++  java
  • JUNIT -- springMVC的action进行单元测试

    原文:http://blog.csdn.net/gaopeng0071/article/details/49946575

    我开发环境springMVC版本3.0.4

    样例代码:

    package com.redhorse.agent.action.junit;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import org.junit.Assert;
    import org.junit.BeforeClass;
    import org.junit.Test;
    import org.springframework.mock.web.MockHttpServletRequest;
    import org.springframework.mock.web.MockHttpServletResponse;
    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;
    
    import com.redhorse.bean.AgentEmployeeBean;
    
    public class EmployeeManagerActionTest {
        private static HandlerMapping handlerMapping;
        private static HandlerAdapter handlerAdapter;
    
        /**
         * 读取spring3 MVC配置文件
         */
        @BeforeClass
        public static void setUp() {
            if (handlerMapping == null) {
                String[] configs = { "file:src/spring/*.xml","file:src/spring/servlet/*.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]);
            }
        }
    
        /**
         * 执行request对象请求的action
         * 
         * @param request
         * @param response
         * @return
         * @throws Exception
         */
        public ModelAndView excuteAction(HttpServletRequest request, HttpServletResponse response) throws Exception {
            HandlerExecutionChain chain = handlerMapping.getHandler(request);
            final ModelAndView model = handlerAdapter.handle(request, response, chain.getHandler());
            return model;
        }
    
        @Test
        public void save() throws Exception {
            MockHttpServletRequest request = new MockHttpServletRequest();
            MockHttpServletResponse response = new MockHttpServletResponse();
            AgentEmployeeBean ae = new AgentEmployeeBean();
            ae.setId(12);
            request.getSession().setAttribute("curUser", ae);
            request.setRequestURI("/employee/addEmployee");
            request.addParameter("email", "gpqhl0071@126.com");
            request.addParameter("mobile", "15652377458");
            request.addParameter("realName", "gao");
            request.addParameter("subRegionIds", "1");
            request.addParameter("end", "1000");
            request.addParameter("point", "10");
            request.setMethod("POST");
            // 执行URI对应的action
            final ModelAndView mav = this.excuteAction(request, response);
            // Assert logic
            Assert.assertEquals("/employee/addEmployee", mav.getViewName());
            String msg = (String) request.getAttribute("msg");
            System.out.println(msg);
        }
    
    }

    需要改动的代码: 
    33行,按照自己要读取的spring配置进行修改。 
    61到81行,写自己的单元测试方法。

  • 相关阅读:
    CMDB服务器管理系统【s5day92】:服务器管理回顾
    主机管理+堡垒机系统开发:需求讨论(一)
    saltstack主机管理项目:今日总结(六)
    saltstack主机管理项目:动态调用插件解析-模块解析(五)
    saltstack主机管理项目:编写插件基类-获取主机列表-提取yaml配置文件(四)
    saltstack主机管理项目:计主机管理项目命令分发器(三)
    saltstack主机管理项目:主机管理项目需求分析(一)
    saltstack主机管理项目:主机管理项目架构设计(二)
    CMDB服务器管理系统【s5day89】:采集资产之整合资产
    CMDB服务器管理系统【s5day89】:采集资产之汇报信息
  • 原文地址:https://www.cnblogs.com/shihaiming/p/6703157.html
Copyright © 2011-2022 走看看