使用Spring MockMVC对controller做单元测试
1、对单一controller做测试。
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
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.setup.MockMvcBuilders;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration; @RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration("classpath:")
@ContextConfiguration("/data/spring-test.xml")
public class CommonCtrlTest { private MockMvc mockMvc; @Autowired
CommonCtrl commonCtrl; @Before
public void setup() {
this.mockMvc = MockMvcBuilders.standaloneSetup(commonCtrl).build();
} @Test
public void testHello() throws Exception {
ResultActions resultActions = this.mockMvc.perform(
MockMvcRequestBuilders.get("/test").accept(MediaType.APPLICATION_JSON));
MvcResult mvcResult = resultActions.andReturn();
String result = mvcResult.getResponse().getContentAsString();
System.out.println("response:" + result);
}
}
2、对整个环境做测试,包括Interceptor。
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import outfox.course.weixinkaoshen.dao.AbstractTest; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; public class TestKaoshenCtrl extends AbstractTest{
@Autowired
private WebApplicationContext wac; private MockMvc mockMvc; @Before
public void setUp() {
mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
} @Test
public void testAddUser() throws Exception {
mockMvc.perform((get("/test/get").param("id", "1")))
.andExpect(status().isOk()).andDo(print());
}
}
使用Spring MockMVC对controller做单元测试的更多相关文章
- 使用Spring MockMVC对controller做单元测试(转)
https://www.cnblogs.com/ylty/p/6420738.html 1.对单一controller做测试. import org.junit.Before; import org. ...
- MockMvc 进行 controller层单元测试 事务自动回滚 完整实例
package com.ieou.ms_backend.controller; import com.google.gson.Gson; import com.ieou.ms_backend.dto. ...
- 如何对Spring MVC中的Controller进行单元测试
对Controller进行单元测试是Spring框架原生就支持的能力,它可以模拟HTTP客户端发起对服务地址的请求,可以不用借助于诸如Postman这样的外部工具就能完成对接口的测试. 具体来讲,是由 ...
- spring junit 做单元测试,报 Failed to load ApplicationContext 错误
spring junit 做单元测试,报 Failed to load ApplicationContext 错误. 查找了好一会,最后发现.@ContextConfiguration(locatio ...
- Spring Boot 2 实践记录之 封装依赖及尽可能不创建静态方法以避免在 Service 和 Controller 的单元测试中使用 Powermock
在前面的文章中(Spring Boot 2 实践记录之 Powermock 和 SpringBootTest)提到了使用 Powermock 结合 SpringBootTest.WebMvcTest ...
- 2539-SpringSecurity系列--在有安全验证的情况下做单元测试Test
在有安全验证的情况下做单元测试Test 版本信息 <parent> <groupId>org.springframework.boot</groupId> < ...
- Spring mvc框架 controller间跳转 ,重定向 ,传参
一.需求背景 1. 需求:spring MVC框架controller间跳转,需重定向.有几种情况:不带参数跳转,带参数拼接url形式跳转,带参数不拼接参数跳转,页面也能显示. @Req ...
- Spring MVC Test -Controller
http://www.petrikainulainen.net/programming/spring-framework/unit-testing-of-spring-mvc-controllers- ...
- 使用MockMvc测试controller
之前我们测试controller的时候仅仅是作为一个pojo来进行简单的测试,spring3.2后我们可以按照控制器的方式来测试Spring MVC的controller了,这样的话在测试控制器的时候 ...
随机推荐
- 读懂jstack
1 jstack jstack是JDK自带的一种线程栈跟踪工具,用于生成java虚拟机当前时刻线程快照.在定位线程卡顿.死锁.block等原因的时候非常有用.使用方法是: jstack [-l] pi ...
- 如何在 Windows 和 Linux 上查找哪个线程使用的 CPU 时 间最长?
使用 jstack 找出消耗 CPU 最多的线程代码
- css浮动的"巨坑"与完美解决办法
浮动 1 浮动概念 如果想实现网页中排版布局,比如一行内显示对应的标签元素,可以使用浮动属性.浮动可以实现元素并排. 块转行内日块也可以实现一行显示,不过存在空白折叠现象 float 浮动 属性值 描 ...
- 学习Kvm(六)
五,管理虚拟存储 5.1 虚拟磁盘概述 5.1.1 虚拟化项目中存储的注意事项 [x] 存储的性能几乎总是虚拟化的瓶颈 [x] 通过多个硬盘驱动以分布磁盘I/O来实现存储解决方案 [x] 考虑部署集中 ...
- Tomcat安装流程(无图简易)
笔记:web->3小结
- 遇到的问题之“解决tomcat中文乱码问题”
方案1:在server.xml中添加了 URIEncoding="UTF-8" 属性 路径:C:\stop\apache-tomcat-8.5.69\conf 修改内容 如若方案1 ...
- js技术之获取字符串中某个字符的位置(lastIndexOf()方法 和 indexOf() 方法)
一.lastIndexOf()方法 1.作用: 方法可返回 一个指定的字符串值 在字符串中最后出现的位置.从左往右查. 2.返回: 一个正整数.或者 -1. 3.语法: stringObject.la ...
- 位运算符 按位与 &——整数n的二进制数中1的个数
整数n的二进制数中1的个数 编写一个函数,输入是一个整数,返回其二进制表达式中数字位数为 '1' 的个数 代码如下: int func(int n)//char ch { int count = 0; ...
- PC端操作系统、移动端操作系统、嵌入式操作系统
左侧部分已是历史的操作系统,右侧的还是活跃的操作系统.安卓系统Android 是Google开发的基于Linux平台的开源手机操作系统.它包括操作系统.用户界面和应用程序-- 移动电话工作所需的全部软 ...
- 正则系列——JavaScript正则表达式入门心得
我发现有个别字符被这个编辑器给刷掉了,但是灰色区域显示正常,以灰色区域代码为准 什么玩意? 在我刚开始学习编程的时候,就听过正则了,也听说正则很牛逼,懂正则的更牛逼.但是苦于没有人指点,也没有使用正则 ...