Spring MVC Mock demo
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<String,String> 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);
} }
Spring MVC Mock demo的更多相关文章
- Spring MVC入门Demo
1 参考http://blog.csdn.net/haishu_zheng/article/details/51490299,用第二种方法创建一个名为springmvcdemo的Maven工程. 2 ...
- Spring Mvc 入门Demo
1.web.xml配置 <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns=" ...
- Spring Mvc 用Demo去学习
1:首先大体知道 SpringMVC 框架的 运行原理(图片来自网络 ) 2:SpringMVC 是依照DispatcherServlet 展开的 这里可以约Structs2对比,structs2 是 ...
- 用intellj 建一个spring mvc 项目DEMO
spring的起初可能经常碰壁,因为网上的资料都是混乱的xml堆成的,混乱难以理解,我这个也是,阿哈哈哈哈! 新建一个Maven->create from archetype->org.j ...
- Spring MVC 单元测试Demo
@RunWith(SpringJUnit4ClassRunner.class) @WebAppConfiguration @ContextConfiguration(locations={" ...
- spring mvc velocity多视图
1.ViewResolverUrlBasedViewResolver 这个东西是根据url 进行路由的.网上搜了 1.order 排序,同名出现各种问题 2.XmlViewResolver,BeanN ...
- Spring MVC 5 + Thymeleaf 基于Java配置和注解配置
Spring MVC 5 + Thymeleaf 注解配置 Spring的配置方式一般为两种:XML配置和注解配置 Spring从3.0开始以后,推荐使用注解配置,这两种配置的优缺点说的人很多,我就不 ...
- Spring MVC如何测试Controller(使用springmvc mock测试)
在springmvc中一般的测试用例都是测试service层,今天我来演示下如何使用springmvc mock直接测试controller层代码. 1.什么是mock测试? mock测试就是在测试过 ...
- Spring 之 @ComponentScan以及mock Spring MVC
[ @ComponentScan] 纠正:可以成功 Autowired 的原因是我在另外一个 config 文件中扫描了根包,这会顺带扫描所有该包的子包 还有,,上面的写法容易出错,建议这样写, @C ...
随机推荐
- LibreOJ β Round #2 F. 数学上来先打表
传送门 题解 做法与题解基本无异,不过他说用vector我觉得用链表来得更好一些. #include<queue> #include<ctime> #include<bi ...
- Codeforces Round #408 (Div. 2)(A.水,B,模拟)
A. Buying A House time limit per test:2 seconds memory limit per test:256 megabytes input:standard i ...
- qsc oj 22 哗啦啦村的刁难(3)(随机数,神题)
哗啦啦村的刁难(3) 发布时间: 2017年2月28日 20:00 最后更新: 2017年2月28日 20:01 时间限制: 1000ms 内存限制: 128M 描述 哗啦啦村作为喵哈哈村 ...
- powerdesign
- MongoDB模拟多文档事务操作
Mongodb不支持多文档原子性操作,因此依据两阶段提交协议(Two Phase Commits protocol)来模拟事务. 以两个银行账户之间的转账行为为例,来说明如何实现多文档间的事务操作. ...
- Vue-cli搭建完项目,各文件解释
脚手架vue-cli搭建完成后,会生成一些文件,总结学习一下这些文件是做什么用的:1.一级目录: build和config文件夹是wbepack配置的文件夹: node_modules是在我npm i ...
- ceph-deploy出错UnableToResolveError Unable to resolve host
背景 ps:在本文中,假设我系统的hostname为node1. 使用ceph-deploy命令搭建Ceph集群,执行ceph new node1时,出现如下错误: [node1][INFO ] Ru ...
- C#历年来最受欢迎功能
不定时更新翻译系列,此系列更新毫无时间规律,文笔菜翻译菜求各位看官老爷们轻喷,如觉得我翻译有问题请挪步原博客地址 本博文翻译自: http://www.dotnetcurry.com/csharp/1 ...
- [国嵌笔记][011][Linux密码破解]
破解步骤 1.在系统启动时进入grub选项菜单 2.在grub选项菜单中按e进入编辑模式 3.编辑kernel行,添加 /init 1 (表示进入单用户启动模式,在单用户启动模式中不会要求输入密码) ...
- [学习OpenCV攻略][013][Mat - 基本图像容器]
Mat 是一个类,由两个数据部分组成:矩阵头(包含矩阵尺寸,存储方法,存储地址等信息)和一个指向存储所有像素值的矩阵(根据所选存储方法的不同矩阵可以是不同的维数)的指针. 矩阵属于多个 Mat 对象, ...