使用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了,这样的话在测试控制器的时候 ...
随机推荐
- LG3978 【[TJOI2015]概率论】
前置:卡特兰数 记\(C_n\)为\(n\)个节点的二叉树的个数,\(C_0=1\),对于\(n \geq 1\),取一个根节点,枚举其左子树大小,有 \[C_n=\sum_{i=0}^{n-1}C_ ...
- shell excute mongo query command
use shell command method one: #!/bin/bash ] then echo 'Please input cid' exit fi HOST= mongo ${HOST} ...
- setjmp与longjmp非局部跳转函数的使用
[root@bogon code]# cat c.c #include<stdio.h> #include<setjmp.h> static jmp_buf env;//定义全 ...
- MySQL 5.7--复制延迟监控
========================================== SHOW PROCESSLIST方式 为保证二进制日志在从库的执行时间和顺序的正确性,二进制日志中的每个语句都设置 ...
- openresty 编译ngx_pagespeed 模块-docker 构建
ngx_pagespeed 是一个很不错的web 优化模块,我们通过简单的配置就可以对于web页面的加载有很大的提升 ngx_pagespeed 依赖psol 模块 Dockerfile # Do ...
- C# to IL 1 Introduction to Microsoft’s IL(MSIL 介绍)
The code that we write in a programming language like C#, ASP+ or in any other .NETcompatible langua ...
- JSON数据的处理中的特殊字符
JSON现在是很常见的处理数据的方式了.但由于自己使用的是反射获取数据,必须自己处理特殊字符,但总是发现有一些看不见的字符在前台 var obj = jQuery.parseJSON(msg);会转换 ...
- oracle-pl/sql之二
java 触发器 包 你可以编写用户定义的函数(用pl/sql,java,c)来提供在sql中或sql内置函数中不可用的功能 有时,我们会发现有些功能通过PL/SQL完成会很麻烦,而通过C/C++语言 ...
- Could not get lock /var/lib/dpkg/lock的解决方法
E: Could not get lock /var/lib/dpkg/lock - open (11: Resource temporarily unavailable) E: Unable to ...
- hadoop mapreduce 简单例子
本例子统计 用空格分开的单词出现数量( 这个Main.mian 启动方式是hadoop 2.0 的写法.1.0 不一样 ) 目录结构: 使用的 maven : 下面是maven 依赖. <de ...