spring中用MockMvc做单元测试

3 篇文章 0 订阅

转自  http://blog.csdn.net/pengoneeast/article/details/46890581


一般测试驱动开发项目中,我们会在Maven中建立一个项目文件和一个一一对应的测试项目文件。每写好一个模块,先对其进行单元测试,再集成到现有的系统中。

针对Controller、Service、Dao三层架构来说,我们最常对Service和Dao进行单元测试。然而Controller的测试,很多人还是启动Tomcat,进行接口测试,这样不紧需要等待很长的编译部署时间而且无法逐个Controller功能进行单独测试,因此特意总结Controller层的单元测试。

技术选型:SpringMVC框架 。SpringMVC已经继承了单元测试的类

1.创建普通类文件

2.引入spring单元测试注释

[java]  view plain  copy
  1. @RunWith(SpringJUnit4ClassRunner.class)  //此处调用Spring单元测试类  
  2. @WebAppConfiguration    //调用javaWEB的组件,比如自动注入ServletContext Bean等等  
  3. @ContextConfiguration(locations = {"classpath:spring-context.xml","classpath:spring-mvc.xml"})//加载Spring配置文件  
  4. public class HealthArticleControllerTest {...代码片段 }//测试代码类  


注意:

@WebAppConfiguration如果不加的话,是没法调用WEB的一些特性的。经常会被遗忘掉。。。

@ContextConfiguration中,需要把所有Spring的配置文件全部加载进来,因为有的项目中Spring 的xml配置是分拆的。 此处的xml是放在resources的根目录中。


3.引入spring注解后,Controller的单元测试需要模拟Server的运行,需要在class中进行WEB环境的初始化。

[java]  view plain  copy
  1. @Autowired  
  2. HealthArticleController healthArticleController;  
  3.   
  4. @Autowired  
  5. ServletContext context;  
  6.   
  7. MockMvc mockMvc;  
  8. @Before  
  9. public void setup(){  
  10.     mockMvc = MockMvcBuilders.standaloneSetup(healthArticleController).build();  
  11. }  
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.前期准备工作都做好了。可以编写单元测试方法了。

[java]  view plain  copy
  1. <span style="font-family: Arial, Helvetica, sans-serif;">  
  2. </span>  
[java]  view plain  copy
  1. <span style="font-family: Arial, Helvetica, sans-serif;">@org.junit.Test  
  2.     public void getArticleListTest(){  
  3. </span>  
[java]  view plain  copy
  1. <span style="font-family: Arial, Helvetica, sans-serif;"><span style="white-space:pre">     </span>String postJson = healthArticleController.findPage();</span>  
[java]  view plain  copy
  1. <span style="font-family: Arial, Helvetica, sans-serif;"><span style="white-space:pre">     </span>//发送请求</span>  
[java]  view plain  copy
  1. ResultActions resultActions = this.mockMvc.perform(MockMvcRequestBuilders.post("/cms/app/getArticleList").accept(MediaType.APPLICATION_JSON).param("criJson",postJson));  
  2. MvcResult mvcResult = resultActions.andReturn();  
  3. String result = mvcResult.getResponse().getContentAsString();  
  4. System.out.println("=====客户端获得反馈数据:" + result);  
[java]  view plain  copy
  1. }  

ResultAction是用来模拟Browser发送FORM表单请求的。post()是请求的地址,accept()请求的内容 param()请求的键值对,如果有多个参数可以后缀调用多个param()

MvcResult是获得服务器的Response内容。


5.到此,整个完整的Controller单元测试完成。贴上我完整的代码

[java]  view plain  copy
  1. package com.chinare.health.modules.cms;  
  2.   
  3. import org.junit.Before;  
  4. import org.junit.runner.RunWith;  
  5. import org.springframework.beans.factory.annotation.Autowired;  
  6. import org.springframework.http.MediaType;  
  7. import org.springframework.test.context.ContextConfiguration;  
  8. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;  
  9. import org.springframework.test.context.web.WebAppConfiguration;  
  10. import org.springframework.test.web.servlet.MockMvc;  
  11. import org.springframework.test.web.servlet.MvcResult;  
  12. import org.springframework.test.web.servlet.ResultActions;  
  13. import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;  
  14. import org.springframework.test.web.servlet.setup.MockMvcBuilders;  
  15.   
  16. import javax.servlet.ServletContext;  
  17. import java.util.Map;  
  18.   
  19. /** 
  20.  * author: Samuel 
  21.  * Date:2015-07-04 
  22.  */  
  23.   
  24. @RunWith(SpringJUnit4ClassRunner.class)  
  25. @WebAppConfiguration  
  26. @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"})  
  27. public class HealthArticleControllerTest {  
  28.   
  29.     @Autowired  
  30.     HealthArticleController healthArticleController;  
  31.   
  32.     @Autowired  
  33.     ServletContext context;  
  34.   
  35.     MockMvc mockMvc;  
  36.     @Before  
  37.     public void setup(){  
  38.         mockMvc = MockMvcBuilders.standaloneSetup(context).build();  
  39.     }  
  40.   
  41.     @org.junit.Test  
  42.     public void getArticleListTest() throws Exception{  
  43.         //准备参数  
  44.         String <span style="font-family: Arial, Helvetica, sans-serif;">postJson </span>= "{\"mac\":\"h\",\"dtClient\":\"2015-06-03 13:20:10\"}";  
  45.           
  46.         //发送请求  
  47.         ResultActions resultActions = this.mockMvc.perform(MockMvcRequestBuilders.post("/cms/app/getArticleList")  
  48.                 .accept(MediaType.APPLICATION_JSON).param("criJson",postJson));  
  49.         MvcResult mvcResult = resultActions.andReturn();  
  50.         String result = mvcResult.getResponse().getContentAsString();  
  51.         System.out.println("=====客户端获得反馈数据:" + result);  
  52.   
  53.           
  54.     }  
  55.   
  56.   
  57.   
  58. }  


遇到的异常 

http://blog.csdn.net/a35038438/article/details/50216673

http://blog.csdn.net/bian1729183741/article/details/47420031

http://www.cnblogs.com/ningheshutong/p/6478080.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值