博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring MVC Mock demo
阅读量:7045 次
发布时间:2019-06-28

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

package com.niwodai.mem.web.controller;import com.alibaba.fastjson.JSON;import org.junit.Before;import org.junit.Test;import org.junit.runner.RunWith;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.mock.web.MockHttpServletResponse;import org.springframework.test.context.ActiveProfiles;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.setup.MockMvcBuilders;import org.springframework.web.context.WebApplicationContext;import java.util.HashMap;import java.util.Map;import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;/** * @Description: * @Author: zhaobo * @Date: 2017/10/17 */@RunWith(SpringJUnit4ClassRunner.class)@WebAppConfiguration@ContextConfiguration(locations={"classpath:applicationContext.xml"})@ActiveProfiles("dev")public class MemGradeControllerTest {    private Logger logger = LoggerFactory.getLogger(MemGradeControllerTest.class);    @SuppressWarnings("SpringJavaAutowiringInspection")    @Autowired    private WebApplicationContext wac;    private MockMvc mockMvc;    @Before    public void startUp(){        mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();    }    @Test    public void mock_memGrade_querySpecRights() throws Exception {        String requestUrl = "/XXXXX";        Map
queryParam = new HashMap<>(); queryParam.put("name","TQ18"); queryParam.put("offset","0"); queryParam.put("limit","10"); String jsonContent = JSON.toJSONString(queryParam); MvcResult result = mockMvc.perform(post(requestUrl) .content(jsonContent)) .andReturn(); MockHttpServletResponse response = result.getResponse(); String resultContent = response.getContentAsString(); logger.info("@@"+resultContent); }}

 

转载于:https://www.cnblogs.com/mengjianzhou/p/7738041.html

你可能感兴趣的文章
android KeyEvent for dot "."
查看>>
第九篇:使用 AdaBoost 元算法提高分类器性能
查看>>
你的身份信息已失效,请重新输入密码登录
查看>>
jQuery------$.each()遍历类方法
查看>>
Serviceability
查看>>
Gson、FastJson、json-lib对比与实例
查看>>
[i.MX6q]i.MX6q处理器,linux操作系统平台搭建 从SD卡启动系统
查看>>
I.MX6 FFmpeg 录制视频
查看>>
伪类元素实现可伸缩时间轴
查看>>
Javscript调用iframe框架页面中函数的方法
查看>>
Java程序员应该了解的10个设计原则
查看>>
Cocos2d-x 3.x游戏开发之旅 笔记
查看>>
如何使用易我数据恢复向导恢复数码相机删除的图片
查看>>
Android系统篇之—-编写系统服务并且将其编译到系统源码中【转】
查看>>
External component has thrown an exception
查看>>
SQLyog之MySQL客户端的下载、安装和使用(旗舰版)(推荐)
查看>>
shadowMap
查看>>
使用JQuery获取被选中的checkbox的value值 以及全选、反选
查看>>
【ztree系列——图标的修改】Bootstrap风格的ztree
查看>>
lua去掉字符串中的UTF-8的BOM三个字节
查看>>