使用Mock 测试 controller层
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层的更多相关文章
- PowerMock+SpringMVC整合并测试Controller层方法
PowerMock扩展自Mockito,实现了Mockito不支持的模拟形式的单元测试.PowerMock实现了对静态方法.构造函数.私有方法以及final方法的模拟支持,对静态初始化过程的移除等强大 ...
- Junit mockito 测试Controller层方法有Pageable异常
1.问题 在使用MockMVC+Mockito模拟Service层返回的时候,当我们在Controller层中参数方法调用有Pageable对象的时候,我们会发现,我们没办法生成一个Pageable的 ...
- Spring自带mock测试Controller
原文:http://blog.csdn.net/yin_jw/article/details/24726941 准备SpringMVC环境 注意:使用mock测试需要引入spring-test包 Ba ...
- SpringBoot测试Controller层
一.准备工作 1.导入测试依赖 <dependency> <groupId>org.springframework.boot</groupId> <artif ...
- junit基础学习之-测试controller层(2)
准备工作: eclipse本身带有junit4,可以直接build path,加入junit. 连接数据库的配置文件需要修改,之前的文件是采用properties+xml文件的形式,但是在测试的时候因 ...
- Spring MVC如何测试Controller(使用springmvc mock测试)
在springmvc中一般的测试用例都是测试service层,今天我来演示下如何使用springmvc mock直接测试controller层代码. 1.什么是mock测试? mock测试就是在测试过 ...
- ssm框架中Controller层的junit测试_我改
Controller测试和一般其他层的junit测试可以共用一个BaseTest 一.BaseTest如下: @RunWith(SpringJUnit4ClassRunner.class) @WebA ...
- mock测试SpringMVC controller报错
使用mock测试Controller时报错如下 java.lang.NoClassDefFoundError: javax/servlet/SessionCookieConfig at org.spr ...
- Spring+Junit+Mock测试web项目,即Controller
准备:Maven依赖 <!-- Spring和MVC的包这里不列出来了,webmvc,aspects,orm,其他maven会自动导 --> <dependency> < ...
随机推荐
- ini文件读写 保存上次存储内容
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- Ping-Pong (Easy Version)的解析
原题链接:http://codeforces.com/problemset/problem/320/B 之前自己做的时候一直读不懂题意,看了大佬的博客才知道是用dfs写,一道暴力搜索大水题https: ...
- mysql主从同步监控---邮件告警
#!/bin/bash #check MySQL_Slave Status #crontab time : MYSQLPORT=`netstat -na|grep "|awk -F[:&qu ...
- Waiting (TTFB) 时间
什么是 Waiting (TTFB) 时间 TTFB 是 Time to First Byte 的缩写,指的是浏览器开始收到服务器响应数据的时间(后台处理时间+重定向时间),是反映服务端响应速度的重要 ...
- MFC 模态对话框、非模态对话框
modal dialogs与modeless dialogs,modal dialogs会让原窗体不被选中,modeless dialogs原窗体依然可以选中. 1.模态对话框的显示 DlgModal ...
- UnhandledExceptionPolicy
winform程序未捕获异常解决方法 EventType clr20r3 P1 http://blog.csdn.net/chichaodechao/article/details/8294922
- [Luogu] 飞扬的小鸟
https://www.luogu.org/problemnew/show/P1941 Bfs or Dp #include <bits/stdc++.h> using namespace ...
- delphi将两个Strlist合并,求交集 (保留相同的)
Function StrList_Join(StrListA,StrListB:String):String; //将两个Strlist合并,求交集 (保留相同的) var SListA,SListB ...
- 删除线性表中为x的元素的三种简单算法。
//删除线性表中不为x的元素. void delete_list(Sqlist &L,int x){ ; ;i < L.length;i++){ if(L.data[i] != x){ ...
- leetcode题目234.回文链表(快慢指针+辅助空间-简单)
题目描述: 请判断一个链表是否为回文链表. 示例 1: 输入: 1->2 输出: false 示例 2: 输入: 1->2->2->1 输出: true 进阶: 你能否用 O( ...