spring 集成测试中对mock 的集成实在是太棒了!但是使用请注意一下3个条件。
 
  • junit 必须使用4.9以上
  • 同时您的框架必须是用spring mvc
  • spring 3.2以上才完美支持
 
目前使用spring MVC 取代struts2 的很多,spring MVC 的各种灵活让人无比销魂!所以使用spring MVC吧!
以前在对接口(主要是java服务端提供的接口(一般是:webService,restful))进行测试的中一般用以下俩种方法。测试流程如图:


 
1 直接使用httpClient 
    这方法各种麻烦
 
2 使用Spring 提供的RestTemplate
    错误不好跟踪,必须开着服务器
 
3 使用mockMVC都不是问题了看使用实例:
 
使用事例如下:父类
 
  1. import  org.junit.runner.RunWith;
  2. import  org.springframework.beans.factory.annotation.Autowired;
  3. import  org.springframework.test.context.ContextConfiguration;
  4. import  org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  5. import  org.springframework.test.context.web.WebAppConfiguration;
  6. import  org.springframework.web.context.WebApplicationContext;
  7. @WebAppConfiguration
  8. @ContextConfiguration (locations = {  "classpath:applicationContext.xml" ,
  9. "classpath:xxxx-servlet.xml"  })
  10. public class  AbstractContextControllerTests {
  11. @Autowired
  12. protected  WebApplicationContext wac;
  13. }
  14. 子类:
  15. import  org.junit.Before;
  16. import  org.junit.Test;
  17. import  org.junit.runner.RunWith;
  18. import  org.springframework.beans.factory.annotation.Autowired;
  19. import  org.springframework.http.MediaType;
  20. import  org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  21. import  org.springframework.test.web.servlet.MockMvc;
  22. import  org.springframework.test.web.servlet.setup.MockMvcBuilders;
  23. import  com.conlect.oatos.dto.status.RESTurl;
  24. import  com.qycloud.oatos.server.service.PersonalDiskService;
  25. //这个必须使用junit4.9以上才有。
  26. @RunWith (SpringJUnit4ClassRunner. class )
  27. public class  PersonalDiskMockTests  extends  AbstractContextControllerTests {
  28. private static  String URI = RESTurl.searchPersonalFile;
  29. private  MockMvc mockMvc;
  30. private  String json = "{\"entId\":1234,\"userId\":1235,\"key\":\"new\"}" ;
  31. @Autowired
  32. private  PersonalDiskService personalDiskService;
  33. @Before
  34. public void  setup() {
  35. //this.mockMvc = webAppContextSetup(this.wac).alwaysExpect(status().isOk()).build();
  36. this .mockMvc = MockMvcBuilders.standaloneSetup(personalDiskService).build();
  37. }
  38. @Test
  39. public void  readJson()  throws  Exception {
  40. this .mockMvc.perform(
  41. post(URI,  "json" ).characterEncoding( "UTF-8" )
  42. .contentType(MediaType.APPLICATION_JSON)
  43. .content(json.getBytes()))
  44. .andExpect(content().string( "Read from JSON: JavaBean {foo=[bar], fruit=[apple]}" )
  45. );
  46. }
上面是简单的例子,实际使用起来可以稍做修改。记得导入spring -test jar 包。无需额外配置(是不是很方便!)
当然和junit 的assert 一起用的话效果更好。下面贴点例子。copy就能跑。
  1. package  com.qycloud.oatos.server.test.mockmvcTest;
  2. import static  org.junit.Assert.fail;
  3. import static  org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
  4. import  java.io.UnsupportedEncodingException;
  5. import  java.lang.reflect.Field;
  6. import  org.springframework.http.MediaType;
  7. import  org.springframework.test.web.servlet.MockMvc;
  8. import  com.conlect.oatos.dto.status.CommConstants;
  9. import  com.conlect.oatos.dto.status.ErrorType;
  10. import  com.conlect.oatos.http.PojoMapper;
  11. public class  MockUtil {
  12. /**
  13. * mock
  14. *
  15. * @param uri
  16. * @param json
  17. * @return
  18. * @throws UnsupportedEncodingException
  19. * @throws Exception
  20. */
  21. public static  String mock(MockMvc mvc, String uri, String json)
  22. throws  UnsupportedEncodingException, Exception {
  23. return  mvc
  24. .perform(
  25. post(uri,  "json" ).characterEncoding( "UTF-8" )
  26. .contentType(MediaType.APPLICATION_JSON)
  27. .content(json.getBytes())).andReturn()
  28. .getResponse().getContentAsString();
  29. }
  30. /**
  31. *
  32. * @param re 返回值
  33. * @param object 要转换的对象
  34. * @param testName 当前测试的对象
  35. */
  36. public static  <T>  void  check(String re, Class<T> object,String testName) {
  37. System.out.println(re);
  38. if  (ErrorType.error500.toString().equals(re)) {
  39. System.out.println( "-----该接口测试失败:-----"
  40. + testName);
  41. fail(re);
  42. }  else if  (CommConstants.OK_MARK.toString().equals(re)) {
  43. System.out.println( "-----该接口测试成功:-----"
  44. + testName);
  45. } else {
  46. System.out.println( "-----re----- :" +re);
  47. }
  48. if  (object !=  null ) {
  49. if  (re.contains( ":" )) {
  50. try  {
  51. T t = PojoMapper.fromJsonAsObject(re, object);
  52. System.out.println( "-----该接口测试成功:-----"
  53. + testName);
  54. }  catch  (Exception e) {
  55. System.out.println( "-----该接口测试失败:-----"
  56. + testName);
  57. fail(e.getMessage());
  58. }
  59. }
  60. }
  61. }
  62. /**
  63. * 初始化版本信息。每次调用测试用力之前首先更新版本信息
  64. * @param mockMvc
  65. * @param url
  66. * @param fileId
  67. * @param class1
  68. * @return
  69. * @throws UnsupportedEncodingException
  70. * @throws Exception
  71. */
  72. public static  <T> Long updateVersion(MockMvc mockMvc, String url,
  73. Long fileId, Class<T> class1)  throws  UnsupportedEncodingException, Exception {
  74. String re = mock(mockMvc, url, fileId+ "" );
  75. T dto = PojoMapper.fromJsonAsObject(re, class1);
  76. Long version = Long.parseLong(dto.getClass().getMethod( "getVersion" ).invoke(dto).toString());
  77. System.out.println( "version = " +version);
  78. return  version;
  79. }
  80. }

使用如下:

  1. @RunWith (SpringJUnit4ClassRunner. class )
  2. public class  PersonalDiskMockTests  extends  AbstractContextControllerTests {
  3. private  MockMvc mockMvc;
  4. private static  Long entId = 1234L;
  5. private static  Long adminId = 1235L;
  6. @Autowired
  7. private  PersonalDiskService personalDiskService;
  8. @Before
  9. public void  setup() {
  10. this .mockMvc = MockMvcBuilders.standaloneSetup(personalDiskService)
  11. .build();
  12. }
  13. /***
  14. * pass
  15. * 全局搜索企业文件
  16. *
  17. * @throws Exception
  18. */
  19. @Test
  20. public void  searchPersonalFile()  throws  Exception {
  21. SearchFileParamDTO sf =  new  SearchFileParamDTO();
  22. sf.setEntId(entId);
  23. sf.setKey( "li" );
  24. sf.setUserId(adminId);
  25. String json = PojoMapper.toJson(sf);
  26. String re = MockUtil.mock( this .mockMvc, RESTurl.searchPersonalFile,
  27. json);
  28. MockUtil.check(re, SearchPersonalFilesDTO. class ,  "searchPersonalFile" );
  29. }

当然@setup里面是每个@test执行时都会执行一次的,所以有些需要每次都实例化的参数可以放进来

如下:

  1. @Autowired
  2. private  ShareDiskService shareDiskService;
  3. @Before
  4. public void  setup() {
  5. this .mockMvc = MockMvcBuilders.standaloneSetup(shareDiskService)
  6. .build();
  7. try  {
  8. initDatas();
  9. }  catch  (Exception e) {
  10. e.printStackTrace();
  11. }
  12. }
  13. private void  initDatas()  throws  UnsupportedEncodingException, Exception {
  14. FileIdVersion = MockUtil.updateVersion(mockMvc,RESTurl.getShareFileById,FileId,ShareFileDTO. class );
  15. File2IdVersion = MockUtil.updateVersion(mockMvc,RESTurl.getShareFileById,File2Id,ShareFileDTO. class );
  16. oldPicFolderVersion = MockUtil.updateVersion(mockMvc,RESTurl.getShareFolderById,oldPicFolderId,ShareFolderDTO. class );
  17. }
  18. 以上是我摘抄别人的,以下是我在公司用的写法
  19. 测试类:

  20. package cn.com.mcd;

    import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
    import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

    import org.codehaus.jackson.map.ObjectMapper;
    import org.junit.Assert;
    import org.junit.Before;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.http.MediaType;
    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    import org.springframework.test.web.servlet.MockMvc;
    import org.springframework.test.web.servlet.MvcResult;
    import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
    import org.springframework.test.web.servlet.setup.MockMvcBuilders;

    import cn.com.mcd.common.ResultModel;
    import cn.com.mcd.controller.StoreLocalPmController;
    import cn.com.mcd.model.StoreLocalPm;

    @RunWith(SpringJUnit4ClassRunner.class)
    public class TestStoreLocalPm extends BaseControllerTest{
    //@Resource
    //private StoreLocalPmService storeLocalPmService;
    @Autowired
    private StoreLocalPmController storeLocalPmController;
    private MockMvc mockMvc;
    @Before
    public void setup() {
    //this.mockMvc = webAppContextSetup(this.wac).alwaysExpect(status().isOk()).build();
    this.mockMvc = MockMvcBuilders.standaloneSetup(storeLocalPmController).build();
    }
    /**
    * 根据条件查询列表
    */
    //@Test
    public void selectStoreLocalPmList() {
    StoreLocalPm storeLocalPm=new StoreLocalPm();
    Long id=1L;
    storeLocalPm.setId(id);//餐厅编号
    storeLocalPm.setStoreName("");//餐厅名称
    storeLocalPm.setAuditStatus("");//审核状态
    ResultModel resultModel=storeLocalPmController.selectStoreLocalPmList(null,storeLocalPm);
    System.out.print("test.resultModel="+resultModel.getResultCode()+"\n"+resultModel.getResultMsg()+"\n"+resultModel.getResultData());
    }
    /**
    * 根据id查询详情
    * @throws Exception
    */
    @Test
    public void selectByPrimaryKey() throws Exception {
    String url = "/storeLocalPm/selectByPrimaryKey";
    StoreLocalPm storeLocalPm =new StoreLocalPm();
    storeLocalPm.setId(1L);

    ObjectMapper mapper = new ObjectMapper();

    String json = mapper.writeValueAsString(storeLocalPm);


    MvcResult result = (MvcResult) mockMvc.perform(post(url)
    .contentType(MediaType.APPLICATION_JSON)
    .content(json)
    .param("epsToken", "token")
    .accept(MediaType.APPLICATION_JSON))
    .andExpect(status().isOk())
    .andDo(MockMvcResultHandlers.print())
    .andReturn();
    System.out.println("----------"+result.getResponse().getStatus());
    System.out.println("----------"+result.getResponse().getContentAsString());
    //Assert.assertEquals(200, result.getResponse().getStatus());
    //Assert.assertNotNull(result.getResponse().getContentAsString());

    //Long id=1L;
    //ResultModel resultModel=storeLocalPmController.selectByPrimaryKey(id);
    //System.out.print("test.resultModel="+resultModel.getResultCode()+"\n"+
    //resultModel.getResultMsg()+"\n"+resultModel.getResultData());

    }
    }

    1. cotroller
    2. package cn.com.mcd.controller;

      import java.util.List;

      import javax.annotation.Resource;

      import org.slf4j.Logger;
      import org.slf4j.LoggerFactory;
      import org.springframework.stereotype.Controller;
      import org.springframework.web.bind.annotation.ModelAttribute;
      import org.springframework.web.bind.annotation.RequestBody;
      import org.springframework.web.bind.annotation.RequestMapping;
      import org.springframework.web.bind.annotation.RequestMethod;
      import org.springframework.web.bind.annotation.ResponseBody;

      import cn.com.mcd.common.ResultModel;
      import cn.com.mcd.model.KitchenEquipmentPrice;
      import cn.com.mcd.model.StoreLocalPm;
      import cn.com.mcd.service.StoreLocalPmService;
      import cn.com.mcd.util.Constants;
      import cn.com.mcd.util.PagesPojo;

      @Controller
      @RequestMapping("/storeLocalPm")
      public class StoreLocalPmController {
      private static final long serialVersionUID = 4515788554984036250L;
      private static final Logger log = LoggerFactory.getLogger(StoreLocalPmController.class);
      @Resource
      private StoreLocalPmService storeLocalPmService;
      /**
      * query detail by id
      * @param id
      * @return
      */
      @RequestMapping(value = "/selectByPrimaryKey", method = RequestMethod.POST)
      @ResponseBody
      public ResultModel selectByPrimaryKey(@RequestBody StoreLocalPm storeLocalPm) {
      log.info(this.getClass().getName()+".selectByPrimaryKey.start.storeLocalPm="+storeLocalPm);
      ResultModel resultModel = new ResultModel();
      try{
      storeLocalPm=storeLocalPmService.selectByPrimaryKey(storeLocalPm.getId());
      log.info(this.getClass().getName()+".selectByPrimaryKey.success.storeLocalPm="+storeLocalPm);
      resultModel.setResultCode(Constants.SERVICE_SUCCESS_CODE);
      resultModel.setResultMsg(Constants.DATA_BASE_SEARCH_SUCCESS_MSG);
      resultModel.setResultData(storeLocalPm);
      }catch(Exception e){
      resultModel.setResultCode(Constants.SERVICE_ERROR_CODE);
      resultModel.setResultMsg(Constants.DATA_BASE_SEARCH_ERROR_MSG);
      }
      log.info(this.getClass().getName()+".selectByPrimaryKey.end.resultModel="+resultModel);
      return resultModel;
      }

      /**
      * query list by param
      * @param id
      * @return
      */
      @RequestMapping(value = "/selectStoreLocalPmList", method = RequestMethod.GET)
      @ResponseBody
      public ResultModel selectStoreLocalPmList(@ModelAttribute PagesPojo<StoreLocalPm> page,StoreLocalPm storeLocalPm) {
      log.info(this.getClass().getName()+".selectStoreLocalPmList.start.page="+page);
      ResultModel result = new ResultModel();
      try{
      int count = storeLocalPmService.countAll(storeLocalPm);
      page.setTotalRow(count);
      List<StoreLocalPm> list = storeLocalPmService.selectStoreLocalPmList(page);
      page.setPages(list);
      result.setResultCode(Constants.SERVICE_SUCCESS_CODE);
      result.setResultMsg(Constants.DATA_BASE_SEARCH_SUCCESS_MSG);
      result.setResultData(page);
      }catch(Exception e){
      result.setResultCode(Constants.SERVICE_ERROR_CODE);
      result.setResultMsg(Constants.DATA_BASE_SEARCH_ERROR_MSG);
      }
      log.info(this.getClass().getName()+".selectStoreLocalPmList.end.result="+result);
      return result;
      }
      int deleteByPrimaryKey(Long id){
      return 0;

      }

      int insert(StoreLocalPm record) {
      return 0;
      }

      int insertSelective(StoreLocalPm record){
      return 0;

      }

      int updateByPrimaryKeySelective(StoreLocalPm record) {
      return 0;
      }

      int updateByPrimaryKey(StoreLocalPm record) {
      return 0;
      }
      }

如何在springMVC 中对REST服务使用mockmvc 做测试的更多相关文章

  1. 如何在SpringMVC中使用REST风格的url

    如何在SpringMVC中使用REST风格的url 1.url写法: get:/restUrl/{id} post:/restUrl delete:/restUrl/{id} put:/restUrl ...

  2. 如何在ubuntu中启用SSH服务

    如何在ubuntu14.04 中启用SSH服务 开篇科普:  SSH 为 Secure Shell 的缩写,由 IETF 的网络工作小组(Network Working Group)所制定:SSH 为 ...

  3. 如何在Ruby中编写微服务?

    [编者按]本文作者为 Pierpaolo Frasa,文章通过详细的案例,介绍了在Ruby中编写微服务时所需注意的方方面面.系国内 ITOM 管理平台 OneAPM 编译呈现. 最近,大家都认为应当采 ...

  4. 如何在IIS中设置HTTPS服务

    文章:https://support.microsoft.com/en-us/help/324069/how-to-set-up-an-https-service-in-iis 在这个任务中 摘要 为 ...

  5. 如何在SpringMVC中获取request对象

    1.注解法 @Autowired private HttpServletRequest request; <listener> <listener-class> org.spr ...

  6. 如何在BPM中使用REST服务(1):通过程序访问网页内容

    这篇文章主要描述如何通过程序来访问网页内容,这是访问REST服务的基础. 在Java中,我们可以使用HttpUrlConnection类来实现,代码如下. package http.base; imp ...

  7. 如何在 IIS 中设置 HTTPS 服务

    Windows Server2008.IIS7启用CA认证及证书制作完整过程 这篇文章介绍了如何安装证书申请工具: 如何在iis创建证书申请: 如何使用iis申请证书生成的txt文件,在工具中开始申请 ...

  8. 技术干货丨如何在VIPKID中构建MQ服务

    小结: 1. https://mp.weixin.qq.com/s/FQ-DKvQZSP061kqG_qeRjA 文 |李伟 VIPKID数据中间件架构师 交流微信 | datapipeline201 ...

  9. 如何在Linux中关闭apache服务(转)

    ??? 最近在写一个简单的http服务器,调试的时候发现apache服务器也在机器上跑着,所以得先把apache关掉.当时装apache的时候就是用了普通的sudo get,也不知道装到哪儿了.到网上 ...

随机推荐

  1. Linux常用命令学习8---(用户和用户组管理)

    1.用户和用户组     用户和用户组概念        用户:使用操作系统的人(Linux支持多个用户在同一时间登陆同一个操作系统)        用户组:具有相同权限的一组用户(Linux系统中可 ...

  2. wp8 入门到精通 仿美拍评论黑白列表思路

    static bool isbool = false; private void BindGameDelete() { Tile tile = new Tile(); List<Color> ...

  3. 不会全排列算法(Javascript实现),我教你呀!

    今天我很郁闷,在实验室凑合睡了一晚,准备白天大干一场,结果一整天就只做出了一道算法题.看来还是经验不足呀,同志仍需努力呀. 算法题目要求是这样的: Return the number of total ...

  4. mysql在linux下修改存储路径

    通过下面几步即可修改路径,这里的路径都是测试的路径,一般默认安装路径在/var/lib/mysql下,真正配置按照真实路径配置. 1.修改/etc/sysconfig/selinux文件:#SELIN ...

  5. [自动运维]oracle自动备份

    数据是应用的核心部分,程序坏了换台机器重新发布就可以,但数据一旦丢失,造成的损失将不可挽回,程序发布到生产后,数据的备份便显得尤为重要,由于不一定所有的服务均有资金完成高级的备份如RAC和DG,在我们 ...

  6. PHP 数据库操作类:ezSQL

    EZSQL类介绍: 下载地址:http://www.jb51.net/codes/26393.htmlezsql是一个小型的快速的数据库操作类,可以让你很容易地用PHP操作各种数据库( MySQL.o ...

  7. jQuery插件(cookie存值)

    使用cookie插件后,可以很方便地通过cookie对象保存.读取.删除用户的信息,还能通过cookie插件保存用户的浏览记录,它的调用格式为: 保存:$.cookie(key,value):读取:$ ...

  8. 防止 jsp被sql注入的五种方法

    一.SQL注入简介 SQL注入是比较常见的网络攻击方式之一,它不是利用操作系统的BUG来实现攻击,而是针对程序员编程时的疏忽,通过SQL语句,实现无帐号登录,甚至篡改数据库. 二.SQL注入攻击的总体 ...

  9. 如何使用的Ue4自带的SQLiteSupport

    在UE4.6版本加入的模块.可以让开发者使用SQLite数据库.SQlite是个轻量型的本地数据库. 我下面就来介绍一下如何使用这个模块. 第一步:下载SQLite源代码以及SQLite GUI管理工 ...

  10. CodeForces 514B

    B. Han Solo and Lazer Gun time limit per test 1 second memory limit per test 256 megabytes input sta ...