package action;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
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.ResultActions;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext; /**
* Created by duanxinli on 2019/10/16.
*/
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration()
@ContextConfiguration({"classpath:application-context.xml","classpath:application-context-datasource.xml","classpath:springmvc-servlet.xml"})
public class MVCTest { @Autowired
private WebApplicationContext wac; private MockMvc mockMvc;
@Before
public void init(){
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build(); //构造MockMvc
} @Test
public void getshortCutTest() throws Exception {
ResultActions reaction=this.mockMvc.perform(MockMvcRequestBuilders.get("/shortcut/listAll")
.accept(MediaType.APPLICATION_JSON));//返回值接收json
reaction.andExpect(MockMvcResultMatchers.status().isOk());
MvcResult mvcResult =reaction.andReturn();
System.out.println(mvcResult.getResponse().getContentAsString());
} @Test
public void postshortCutTest() throws Exception {
ResultActions reaction =this.mockMvc.perform(MockMvcRequestBuilders.post("/policy/info/save")
.contentType(MediaType.APPLICATION_JSON)//请求体时json
.header("Timestamp", "1496656373791")
.header("AppId", "1003"));
reaction.andExpect(MockMvcResultMatchers.status().isOk());
MvcResult mvcResult =reaction.andReturn();
System.out.println(mvcResult.getResponse().getContentAsString());
} }

使用Mock 测试 controller层的更多相关文章

  1. PowerMock+SpringMVC整合并测试Controller层方法

    PowerMock扩展自Mockito,实现了Mockito不支持的模拟形式的单元测试.PowerMock实现了对静态方法.构造函数.私有方法以及final方法的模拟支持,对静态初始化过程的移除等强大 ...

  2. Junit mockito 测试Controller层方法有Pageable异常

    1.问题 在使用MockMVC+Mockito模拟Service层返回的时候,当我们在Controller层中参数方法调用有Pageable对象的时候,我们会发现,我们没办法生成一个Pageable的 ...

  3. Spring自带mock测试Controller

    原文:http://blog.csdn.net/yin_jw/article/details/24726941 准备SpringMVC环境 注意:使用mock测试需要引入spring-test包 Ba ...

  4. SpringBoot测试Controller层

    一.准备工作 1.导入测试依赖 <dependency> <groupId>org.springframework.boot</groupId> <artif ...

  5. junit基础学习之-测试controller层(2)

    准备工作: eclipse本身带有junit4,可以直接build path,加入junit. 连接数据库的配置文件需要修改,之前的文件是采用properties+xml文件的形式,但是在测试的时候因 ...

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

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

  7. ssm框架中Controller层的junit测试_我改

    Controller测试和一般其他层的junit测试可以共用一个BaseTest 一.BaseTest如下: @RunWith(SpringJUnit4ClassRunner.class) @WebA ...

  8. mock测试SpringMVC controller报错

    使用mock测试Controller时报错如下 java.lang.NoClassDefFoundError: javax/servlet/SessionCookieConfig at org.spr ...

  9. Spring+Junit+Mock测试web项目,即Controller

    准备:Maven依赖 <!-- Spring和MVC的包这里不列出来了,webmvc,aspects,orm,其他maven会自动导 --> <dependency> < ...

随机推荐

  1. linux——系统命令

    (1)   显示系统日期和时间:date 显示系统当前时间 例如:date (1)  切换用户:su 用户名 以其他用户身份使用系统,(类似windows10系统,有些程序以管理员身份执行) ① 从r ...

  2. JDBC下

    存储过程名字前面一定要加一个sp,代表是存储过程 nofilter:没有过滤器,没有参数 )) BEGIN IF sp_name IS NULL OR sp_name='' THEN SELECT * ...

  3. 2018多校第十场 HDU 6430 (线段树合并)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6430 题意:一棵树上每个节点权值为v[i],每个节点的heard值是:以它为LCA的两个节点的GCD的 ...

  4. [React] Create a Persistent Reference to a Value Using React useRef Hook

    The useRef is a hook for creating values that persist across renders. In this lesson we'll learn how ...

  5. Codeforces Round #455 (Div. 2) 909D. Colorful Points

    题 OvO http://codeforces.com/contest/909/problem/D CF 455 div2 D CF 909D 解 算出模拟的复杂度之后就是一个很水的模拟题 把字符串按 ...

  6. Python2.x与3​​.x版本区别Ⅲ

    八进制字面量表示 八进制数必须写成0o777,原来的形式0777不能用了:二进制必须写成0b111. 新增了一个bin()函数用于将一个整https://www.xuanhe.net/数转换成二进制字 ...

  7. O(1)快速乘与O(log)快速乘

    //O(1)快速乘 inline LL quick_mul(LL x,LL y,LL MOD){     x=x%MOD,y=y%MOD;     return ((x*y-(LL)(((long d ...

  8. Noip2016 提高组 Day2 T1 组合数问题

    题目描述 组合数表示的是从n个物品中选出m个物品的方案数.举个例子,从(1,2,3) 三个物品中选择两个物品可以有(1,2),(1,3),(2,3)这三种选择方法.根据组合数的定 义,我们可以给出计算 ...

  9. 7.20T1

    排序(sort) [问题描述] 有 n 个人依次站在小 A 面前.小 A 会依次对这 n 个人进行 m 次操作. 每次操作选择一个位置 k,将这 n 个人中的所有身高小于等于当前 k 位置的 人的身高 ...

  10. 7.19T3

    小 X 的图 题目背景及题意 小 X 有一张图,有 n 个点(从 0 开始编号),一开始图里并没有 边,他有时候会向其中添加一条双向边(x 到 y).小 X 会时不时想知 道某两个点是否联通,如果连通 ...