博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JUNIT -- springMVC的action进行单元测试
阅读量:6690 次
发布时间:2019-06-25

本文共 3500 字,大约阅读时间需要 11 分钟。

原文: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行,按照自己要读取的配置进行修改。 
61到81行,写自己的单元方法。

 

你可能感兴趣的文章
k8s使用deployment升级
查看>>
ionic3项目实战教程 - 第10讲 ionic3分类菜单设计(类似外卖)
查看>>
深度解析 | K8S API Server之入门须知
查看>>
LeanEngine 中使用 WebSocket
查看>>
浅入分析和Linux内核相关的文件夹/proc和/sys .
查看>>
Java 二分查找
查看>>
刚刚,阿里开源了一项重磅炸弹,终结程序员“中年危机”!
查看>>
《Spring Boot开发:从0到1》图片
查看>>
教你十分钟构建好 SpringBoot + SSM 框架
查看>>
吸烟场景运营商“烟客”获2000万元Pre-A轮融资,用于线下吸烟空间建设
查看>>
未来 Docker 的安全
查看>>
基于Android ActionBar的SearchView实时搜索结果
查看>>
spring boot +RabbitMQ +InfluxDB+Grafara监控实践
查看>>
马斯克的另一番“威胁论”:人类将成为人工智能的“宠物”
查看>>
Python 正则表达式(字符)详解
查看>>
Kali Linux 网络扫描秘籍 第三章 端口扫描(一)
查看>>
共享单车步入物联网军备战
查看>>
PHP 魔术变量
查看>>
推荐的PHP编码规范
查看>>
Gartner报告:东方金信进入Hadoop世界厂商名录
查看>>