Spring-Controller单元测试
来源:互联网 发布:互联网公平台用java 编辑:程序博客网 时间:2024/06/06 07:40
一般测试驱动开发项目中,我们会在Maven中建立一个项目文件和一个一一对应的测试项目文件。每写好一个模块,先对其进行单元测试,再集成到现有的系统中。
针对Controller、Service、Dao三层架构来说,我们最常对Service和Dao进行单元测试。然而Controller的测试,很多人还是启动Tomcat,进行接口测试,这样不紧需要等待很长的编译部署时间而且无法逐个Controller功能进行单独测试,因此特意总结Controller层的单元测试。
技术选型:SpringMVC框架 。SpringMVC已经继承了单元测试的类
1.创建普通类文件
2.引入Spring单元测试注释
@RunWith(SpringJUnit4ClassRunner.class) //此处调用Spring单元测试类@WebAppConfiguration//调用javaWEB的组件,比如自动注入ServletContext Bean等等@ContextConfiguration(locations = {"classpath:spring-context.xml","classpath:spring-mvc.xml"})//加载Spring配置文件public class HealthArticleControllerTest {...代码片段 }//测试代码类
@WebAppConfiguration如果不加的话,是没法调用WEB的一些特性的。经常会被遗忘掉。。。
@ContextConfiguration中,需要把所有Spring的配置文件全部加载进来,因为有的项目中Spring 的xml配置是分拆的。 此处的xml是放在resources的根目录中。
3.引入spring注解后,Controller的单元测试需要模拟Server的运行,需要在class中进行WEB环境的初始化。
@Autowired HealthArticleController healthArticleController; @Autowired ServletContext context; MockMvc mockMvc; @Before public void setup(){ mockMvc = MockMvcBuilders.standaloneSetup(healthArticleController).build(); }HealthArticleController是我需要测试的Controller
MockMvc是SpringMVC提供的Controller测试类
每次进行单元测试时,都是预先执行@Before中的setup方法,初始化healthArticleController单元测试环境。
注意:一定要把待测试的Controller实例进行MockMvcBuilders.standaloneSetup(xxxxController).build(); 否则会抛出无法找到@RequestMapping路径的异常:No mapping found for HTTP request with URI [/cms/app/getArticleList] in DispatcherServlet
4.前期准备工作都做好了。可以编写单元测试方法了。
<span style="font-family: Arial, Helvetica, sans-serif;"></span>
<span style="font-family: Arial, Helvetica, sans-serif;">@org.junit.Test public void getArticleListTest(){</span>
<span style="font-family: Arial, Helvetica, sans-serif;"><span style="white-space:pre"></span>String postJson = healthArticleController.findPage();</span>
<span style="font-family: Arial, Helvetica, sans-serif;"><span style="white-space:pre"></span>//发送请求</span>
ResultActions resultActions = this.mockMvc.perform(MockMvcRequestBuilders.post("/cms/app/getArticleList").accept(MediaType.APPLICATION_JSON).param("criJson",postJson)); MvcResult mvcResult = resultActions.andReturn(); String result = mvcResult.getResponse().getContentAsString(); System.out.println("=====客户端获得反馈数据:" + result);
}
ResultAction是用来模拟Browser发送FORM表单请求的。post()是请求的地址,accept()请求的内容 param()请求的键值对,如果有多个参数可以后缀调用多个param()
MvcResult是获得服务器的Response内容。
5.到此,整个完整的Controller单元测试完成。贴上我完整的代码
package com.chinare.health.modules.cms;import org.junit.Before;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.http.MediaType;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import org.springframework.test.context.web.WebAppConfiguration;import org.springframework.test.web.servlet.MockMvc;import org.springframework.test.web.servlet.MvcResult;import org.springframework.test.web.servlet.ResultActions;import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;import org.springframework.test.web.servlet.setup.MockMvcBuilders;import javax.servlet.ServletContext;import java.util.Map;/** * author: Samuel * Date:2015-07-04 */@RunWith(SpringJUnit4ClassRunner.class)@WebAppConfiguration@ContextConfiguration(locations = {"classpath:spring-context.xml", "classpath:spring-context-jedis.xml", "classpath:spring-mvc.xml", "classpath:spring-context-shiro.xml", "classpath:spring-context-activiti.xml"})public class HealthArticleControllerTest { @Autowired HealthArticleController healthArticleController; @Autowired ServletContext context; MockMvc mockMvc; @Before public void setup(){ mockMvc = MockMvcBuilders.standaloneSetup(context).build(); } @org.junit.Test public void getArticleListTest() throws Exception{ //准备参数 String <span style="font-family: Arial, Helvetica, sans-serif;">postJson </span>= "{\"mac\":\"h\",\"dtClient\":\"2015-06-03 13:20:10\"}"; //发送请求 ResultActions resultActions = this.mockMvc.perform(MockMvcRequestBuilders.post("/cms/app/getArticleList") .accept(MediaType.APPLICATION_JSON).param("criJson",postJson)); MvcResult mvcResult = resultActions.andReturn(); String result = mvcResult.getResponse().getContentAsString(); System.out.println("=====客户端获得反馈数据:" + result); }}
- Spring MVC Controller 单元测试
- Spring-Controller单元测试
- Spring-Controller单元测试
- Spring Mvc controller单元测试
- Spring入门21 - Controller的单元测试
- Controller 使用RestTemplate进行单元测试 spring mvc
- Junit对Spring Controller进行单元测试
- spring MVC mock类单元测试(controller)
- 关于Spring中Controller的单元测试
- controller单元测试
- spring MVC controller 的单元测试 第一部分:配置
- spring webmvc如何编写service 和controller的单元测试
- 使用Mock 对spring mvc 的controller层进行单元测试
- Spring对Controller、Service、Dao进行Junit单元测试总结
- Spring对Controller、Service、Dao进行Junit单元测试总结
- Grails中的Controller单元测试
- springmvc controller 单元测试
- 对Controller的单元测试
- 我的书单和简单书评(持续更新)
- properties操作。
- Unity3D研究之与根据动态的两个轨迹点绘制面详解
- 当大数据遇见应用性能管理(概念篇①)
- CoreAnimation编程指南(七)图层Action
- Spring-Controller单元测试
- 通过js、jquery、ajax将php的输出传给html元素
- BIRT --百度百科
- Android SQLite数据库增删改查操作的使用详解
- 高并发解决方案
- Android ListView note
- 单片机与PC机多机通讯 串口通信
- First Missing Positive
- “程序员”学哪种编程语言最赚钱?