使用Spring MockMVC对controller做单元测试(转)
https://www.cnblogs.com/ylty/p/6420738.html
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做单元测试
1.对单一controller做测试. import org.junit.Before; import org.junit.Test; import org.springframework.beans ...
- 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了,这样的话在测试控制器的时候 ...
随机推荐
- 02 http,servlet,servletconfig,HttpServletRequest ,HttpServletResponse
Http协议 协议:双方在交互.通讯的时候, 遵守的一种规范.规则.http协议:针对网络上的客户端 与 服务器端在执行http请求的时候,遵守的一种规范. 其实就是规定了客户端在访问服务器端的时候, ...
- Vs code 设置
{ "git.ignoreMissingGitWarning": true, "terminal.integrated.shell.windows": &quo ...
- [P2051 [AHOI2009]中国象棋] DP
https://www.luogu.org/problemnew/show/P2051 题目描述 这次小可可想解决的难题和中国象棋有关,在一个N行M列的棋盘上,让你放若干个炮(可以是0个),使得没有一 ...
- 【HDOJ4635】【Tarjan缩点+思维】【经典】
http://acm.hdu.edu.cn/showproblem.php?pid=4635 Strongly connected Time Limit: 2000/1000 MS (Java/Oth ...
- 《DSP using MATLAB》Problem 5.30
代码: %% ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ %% Output In ...
- MySQL数据库-数据表、以及列的增删改查
1.创建一个表 CREATE(创建) TABLE(表) ENGINE(引擎) ENGINE=INNODB(引擎)还有很多类引擎,这里只是简单的提一下INNODB引擎,INNODB引擎支持事务(回滚), ...
- LeetCode - Diameter of Binary Tree
Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of a b ...
- python基础(四)——正则表达式
#!/usr/bin/python # -*- coding: utf-8 -*- import re print(re.match('www', 'www.runoob.com').span()) ...
- Java实现选择排序以及冒泡排序
//排序 选择排序 数组中每个元素都进行比较 public class Test { public static void main(String[] args) { int[] arr = {12, ...
- Django中需要注意的点
需要注意的点 请求相关 注销的 用法 def logout(request): request.session.flush()#输入此内容可以注销用户登录信息 # 即将session信息清除掉 ret ...