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 ...
随机推荐
- [POJ2243]考研路茫茫——单词情结
又是AC自动机上用矩乘优化DP= = 其实和上一题基本一样...补集转化思想.. 只是要多弄一个小矩阵求(26^1+26^2+....+26^L),并且也要求f的总和(因为是长度<=L) 直接调 ...
- [bzoj1717][Usaco2006 Dec]Milk Patterns 产奶的模式 (hash构造后缀数组,二分答案)
以后似乎终于不用去学后缀数组的倍增搞法||DC3等blablaSXBK的方法了= = 定义(来自关于后缀数组的那篇国家集训队论文..) 后缀数组:后缀数组SA是一个一维数组,它保存1..n的某个排列S ...
- hdu_2444The Accomodation of Students(二分图的判定和计算)
hdu_2444The Accomodation of Students(二分图的判定和计算) 标签:二分图匹配 题目链接 题意: 问学生是否能分成两部分,每一部分的人都不相认识,如果能分成的话,两两 ...
- 什么是 core dump ? 以及如何使用gdb对 core dumped 进行调试
什么是core dump?(down = 当) core的意思是:内存,dump的意思是:扔出来.堆出来. 开发和使用linux程序时,有时程序莫名其妙的down掉了,却没有任何的提示(有时候会提示c ...
- 【JAVA】SWING_ 界面风格
在java中,界面外观的管理是由UIManager类来管理的.不同的系统上安装的外观不一样 ,默认的是java的跨平台外观. 1.获取系统所有外观 import javax.swing.*; impo ...
- springboot注解使用说明
springboot注解 @RestController和@RequestMapping注解 我们的Example类上使用的第一个注解是 @RestController .这被称为一个构造型(ster ...
- HTML5 Canvas 数据持久化存储之属性列表
属性列表想必大家都不会陌生,正常用 HTML5 来做的属性列表大概就是用下拉菜单之类的,而且很多情况下,下拉列表还不够好看,怎么办?我试着用 HT for Web 来实现属性栏点击按钮弹出多功能选框, ...
- UE4 UnLoadStreamLevel
今天测试发现一个bug记录一下,如果把某一个子关卡的加载方式设置为Always Loaded时,调用UnloadStreamLevel时不会执行Completed后面的节点,也就是一直没有完成.
- jquery ajax 请求中多出现一次OPTIONS请求及其解决办法
http://www.tangshuang.net/2271.html 在上一篇<服务端php解决jquery ajax跨域请求restful api问题及实践>中,我简单介绍了如何通过服 ...
- Apache日志分析_shell命令行
说明: 1.我的日志预先设定好按日生成文件:"CustomLog "|/opt/apache/bin/rotatelogs /opt/apache/logs/www.website ...