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的更多相关文章

  1. Spring MVC入门Demo

    1 参考http://blog.csdn.net/haishu_zheng/article/details/51490299,用第二种方法创建一个名为springmvcdemo的Maven工程. 2  ...

  2. Spring Mvc 入门Demo

    1.web.xml配置 <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns=" ...

  3. Spring Mvc 用Demo去学习

    1:首先大体知道 SpringMVC 框架的 运行原理(图片来自网络 ) 2:SpringMVC 是依照DispatcherServlet 展开的 这里可以约Structs2对比,structs2 是 ...

  4. 用intellj 建一个spring mvc 项目DEMO

    spring的起初可能经常碰壁,因为网上的资料都是混乱的xml堆成的,混乱难以理解,我这个也是,阿哈哈哈哈! 新建一个Maven->create from archetype->org.j ...

  5. Spring MVC 单元测试Demo

    @RunWith(SpringJUnit4ClassRunner.class) @WebAppConfiguration @ContextConfiguration(locations={" ...

  6. spring mvc velocity多视图

    1.ViewResolverUrlBasedViewResolver 这个东西是根据url 进行路由的.网上搜了 1.order 排序,同名出现各种问题 2.XmlViewResolver,BeanN ...

  7. Spring MVC 5 + Thymeleaf 基于Java配置和注解配置

    Spring MVC 5 + Thymeleaf 注解配置 Spring的配置方式一般为两种:XML配置和注解配置 Spring从3.0开始以后,推荐使用注解配置,这两种配置的优缺点说的人很多,我就不 ...

  8. Spring MVC如何测试Controller(使用springmvc mock测试)

    在springmvc中一般的测试用例都是测试service层,今天我来演示下如何使用springmvc mock直接测试controller层代码. 1.什么是mock测试? mock测试就是在测试过 ...

  9. Spring 之 @ComponentScan以及mock Spring MVC

    [ @ComponentScan] 纠正:可以成功 Autowired 的原因是我在另外一个 config 文件中扫描了根包,这会顺带扫描所有该包的子包 还有,,上面的写法容易出错,建议这样写, @C ...

随机推荐

  1. BZOJ 1061: [Noi2008]志愿者招募【单纯形裸题】

    1061: [Noi2008]志愿者招募 Time Limit: 20 Sec  Memory Limit: 162 MBSubmit: 4813  Solved: 2877[Submit][Stat ...

  2. HDU Today(最短路)

    http://acm.hdu.edu.cn/showproblem.php?pid=2112 HDU Today Time Limit: 15000/5000 MS (Java/Others)     ...

  3. zookeeper基本讲解及基本命令和配置 (转)

    一.ZooKeeper概述 ZooKeeper是一种为分布式应用所设计的高可用.高性能且一致的开源协调服务,是Google的Chubby一个开源实现,是Hadoop和Hbase的重要组件,它提供了一项 ...

  4. 微信小程序监听input输入并取值

    小程序的事件分为两种,冒泡和非冒泡事件,像<form/>的submit事件,<input/>的input事件,<scroll-view/>的scroll事件等非冒泡 ...

  5. Myeclipse2014 已有项目更换JDK

    原先项目使用JDK是1.7,今天项目加入了一个新包,只支持JAVA8,让我们都升级一下本地的JDK,我突然发现我还没有在myeclipse上升级过JDK呢.捣鼓了一下,也不难,记录一下. 1.下载ja ...

  6. 浅析@Deprecated,调用方法时出现横线划掉样式

    Deprecated 这个注释是一个标记注释.所谓标记注释,就是在源程序中加入这个标记后,并不影响程序的编译,但有时编译器会显示一些警告信息. 那么Deprecated注释是什么意思呢?如果你经常使用 ...

  7. Spark算子--coalesce和repartition

    coalesce和repartition--Transformation类算子 代码示例

  8. 【转载】keil5中加入STM32F10X_HD,USE_STDPERIPH_DRIVER的原因

    初学STM32,在RealView MDK 环境中使用STM32固件库建立工程时,初学者可能会遇到编译不通过的问题.出现如下警告或错误提示: warning: #223-D: function &qu ...

  9. vim 的各种用法,很实用哦,都是本人是在工作中学习和总结的

    (一)初级个性化配置你的vim 1.vim是什么? vim是Vi IMproved,是编辑器Vi的一个加强版,一个极其强大并符合IT工程师(程序员.运维)习惯的编辑器.如果你是一名职业的SE,那么一定 ...

  10. JavaScript数组的22种方法

    原文:http://www.cnblogs.com/xiaohuochai/p/5682621.html javascript中数组的22种方法   前面的话 数组总共有22种方法,本文将其分为对象继 ...