原文: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行,写自己的单元测试方法。

JUNIT -- springMVC的action进行单元测试的更多相关文章

  1. springMVC整合Junit4进行单元测试

    springMVC整合Junit4进行单元测试 标签: springMVC整合Junit4junit单元测试教程springMVC入门教程   spring(10)  版权声明:本文为博主原创文章,未 ...

  2. Struts2和SpringMVC的action是单例还是原型的?

    struts2的acion单独使用的时候应是多例的,也就是原型(prototype). 因为它是基于类开发的,它的三种获取页面传参的方式都是通过成员变量的方式来接受的. 如果用struts2框架基于方 ...

  3. 2017.3.31 spring mvc教程(五)Action的单元测试

    学习的博客:http://elf8848.iteye.com/blog/875830/ 我项目中所用的版本:4.2.0.博客的时间比较早,11年的,学习的是Spring3 MVC.不知道版本上有没有变 ...

  4. 学习笔记:首次进行JUnit+Ant构建自动的单元测试(二)

    关键字:JUnit,Ant,单元测试 终于把JUnit+Ant构建单元测试的大概了解了,其实我实践的过程是对了,只是指导博客(看到这里不懂请看我上一篇博客)本身的错误“成功”把我带入“坑”,有时候网友 ...

  5. 学习笔记:首次进行JUnit+Ant构建自动的单元测试(一)

    指导博客:https://blog.csdn.net/Cceking/article/details/51692010 基于软件测试的需求,使用JUnit+Ant构建自动的单元测试. IDE:ecli ...

  6. 使用junit进行最简单的单元测试

    使用junit进行最简单的单元测试 使用工具: jdk IDEA Maven 第一步 创建一个Maven项目 第二步 导入junit依赖 <dependency> <groupId& ...

  7. junit+mock+spring-test构建后台单元测试

    from:从0开始,构建前后端分离应用 1. 一些基本概念 1.1 为什么要进行单元测试?我自己的理解是 1.能够快速发现问题.避免衍生BUG的出现     在对一些现有代码进行修改时,或者修改现有B ...

  8. SpringMVC的Action在同一时间里只允许同一个浏览器的单次进入?

    最近用SpringMVC写了一个很简单的测试程序,代码如下: @Controller public class LongTimeTaskController { @RequestMapping(val ...

  9. 在SpringMVC利用MockMvc进行单元测试

    spring在线文档:https://docs.spring.io/spring/docs/current/javadoc-api/index.html?index-files/index-13.ht ...

随机推荐

  1. 语音行业技术领先者Nuance诚招ASR/NLP研发工程师和软件工程师

    Nuance is a leading provider of voice and language solutions for businesses and consumers around the ...

  2. 用Python爬取智联招聘信息做职业规划

    上学期在实验室发表时写了一个爬取智联招牌信息的爬虫. 操作流程大致分为:信息爬取——数据结构化——存入数据库——所需技能等分词统计——数据可视化 1.数据爬取 job = "通信工程师&qu ...

  3. emacs - GNU Emacs

    总览 (SYNOPSIS) emacs [ command-line switches ] [ files ... ] 描述 (DESCRIPTION) GNU Emacs 是 Emacs 的 一个 ...

  4. Linux目录结构及详细介绍

    /:根目录,位于Linux文件系统目录结构的顶层,一般根目录下只存放目录,不要存放文件,/etc./bin./dev./lib./sbin应该和根目录放置在一个分区中. /bin,/usr/bin:该 ...

  5. QT_6_QMainWindow

    QMainWindow 1.1. 菜单栏 1.1.1. 只有一个 1.1.2. QMenuBar *bar = MenuBar(); 1.1.3. 设置到窗口中 setMenuBar(bar); 1. ...

  6. 筛选法 || POJ 3292 Semi-prime H-numbers

    5,9,13,……叫H-prime 一个数能且仅能由两个H-prime相乘得到,则为H-semi-prime 问1-n中的H-semi-prime有多少个 *解法:vis初始化为0代表H-prime, ...

  7. 微信公众号:theTree20181123

    哈哈哈哈~我开通了一个微信公众号,以后的文章会发在公众号内啦~走过路过的小伙伴们过来围观一下呀~~ 主要是分为三个模块:视觉SLAM,ACM,变美树洞 这里面写下来的文章都是我再读研阶段的所学所想当然 ...

  8. 第1节 flume:6、flume的入门测试案例

    案例:使用网络telent命令向一台机器发送一些网络数据,然后通过flume采集网络端口数据. 1.2.1 Flume的安装部署 第一步:下载解压修改配置文件 Flume的安装非常简单,只需要解压即可 ...

  9. 【简●解】[ZJOI2005]午餐

    [简●解][ZJOI2005]午餐 [关键词] \(DP\) 排序/贪心 [分析] 首先,一个很明显的贪心思路,就是吃的慢的人先打饭.所以把数据按吃饭时间从大到小排一遍序. 根据\(dp\)的尿性,比 ...

  10. python入门-PyCharm中目录directory与包package的区别及相关import详解

    一.概念介绍 在介绍目录directory与包package的区别之前,先理解一个概念---模块 模块的定义:本质就是以.py结尾的python文件,模块的目的是为了其他程序进行引用. 目录(Dict ...