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 ...
随机推荐
- POJ1635:Subway tree systems
链接:http://poj.org/problem?id=1635 填坑树同构 题目给出的是除根外的括号序列表示. 其实只要跟你说hash大家都能写得出来…… hash函数取个效果别太差的就行了吧 # ...
- [bzoj1826] [JSOI2010]缓存交换
虽然不知道为什么..但显然,每次扔掉离下次查询最远的内存单元就行了233 用堆来维护贪心...(优先队列大法好 #include<cstdio> #include<iostream& ...
- HDU1312-Red and Black-DFS
Red and Black Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- Spring学习日志之Spring MVC启动配置
对DispatcherServlet进行配置 Spring MVC的配置实际上就是对DispatcherServlet的配置 public class DispatcherServletConfig ...
- H5基础浏览器兼容性
<!DOCTYPE HTML><html><body> <video width="320" height="240" ...
- C++ 不定参数(转)
转自:http://www.cnblogs.com/jerrychenfly/archive/2010/10/22/1858232.html 下面,我们来看一下,如果在c++的函数中接收数量不定的函数 ...
- PhpStorm常用的一些快捷键
Ctrl + G : 跳到当前打开文件的行,输入格式(行数:列数)Ctrl + E : 打开最近编辑的文件列表Ctrl + P : 显示函数的参数信息Ctrl + Q : 快速打开函数的说明 ...
- 阿里云邮箱POP3、SMTP设置教程
3G免费网www.3gmfw.cn免费为你分享阿里云邮箱POP3.SMTP设置教程,阿里云邮箱 阿里云邮箱POP3设置 阿里云邮箱SMTP设置的相关资源如下: 什么是POP3.SMTP? 阿里云邮箱已 ...
- WOW.js – 让页面滚动更有趣
演示1 演示2-仿oppo首页 下载 简介 有的页面在向下滚动的时候,有些元素会产生细小的动画效果.虽然动画比较小,但却能吸引你的注意.比如刚刚发布的 iPhone 6 的页面(查看).如果你希望你的 ...
- ThinkPHP配置文件的加载
分类: thinkphp(39) 版权声明:本文为博主原创文章,未经博主允许不得转载. 惯例配置->应用配置->模式配置->调试配置->状态配置->模块配置->扩展 ...