spring-mvc springboot 使用MockMvc对controller进行测试
网上基本都是参考官方的使用方式,使用了import static,个人感觉这种方式特别不好,代码提示性不友好。所以在此进行说明,也方便自己以后使用。
1. 引入spring-test相关jar包,springboot只需引入spring-boot-starter-test即可
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
2. 写好controller,开始写test类
import org.front.server.Application;
import org.front.server.web.control.TestController;
import org.hamcrest.Matchers;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.http.MediaType;
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.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
//网上很多会在这里使用import static,主要导入的是MockMvcRequestBuilders,MockMvcResultMatchers,Matchers这三个类中的方法。
/**
* @author zz
* @date 2017年7月4日
*
*/
@RunWith(SpringJUnit4ClassRunner.class)
//@SpringApplicationConfiguration(classes = MockServletContext.class)//这个测试单个controller,不建议使用
@SpringApplicationConfiguration(classes = Application.class)//这里的Application是springboot的启动类名。
@WebAppConfiguration
public class ApplicationTests {
@Autowired
private WebApplicationContext context;
private MockMvc mvc; @Before
public void setUp() throws Exception {
// mvc = MockMvcBuilders.standaloneSetup(new TestController()).build();
mvc = MockMvcBuilders.webAppContextSetup(context).build();//建议使用这种
}
@Test
public void test1() throws Exception {
mvc.perform(MockMvcRequestBuilders.get("/data/getMarkers")
.contentType(MediaType.APPLICATION_JSON_UTF8)
.param("lat", "123.123").param("lon", "456.456")
.accept(MediaType.APPLICATION_JSON))
.andExpect(MockMvcResultMatchers.status().isOk())
.andDo(MockMvcResultHandlers.print())
.andExpect(MockMvcResultMatchers.content().string(Matchers.containsString("SUCCESS"))); }
}
相信这样,基本开发过javaweb的就都能看懂了。通过方法的字面意思应该都能看懂方法含义,如果实在不懂请看源码或者官方API。
spring-mvc springboot 使用MockMvc对controller进行测试的更多相关文章
- spring mvc: 多动作控制器(Controller下面实现多个访问的方法)MultiActionController / BeanNameUrlHandlerMapping
spring mvc: 多动作控制器(Controller下面实现多个访问的方法) 比如我的控制器是UserController.java,下面有home, add, remove等多个方法 访问地址 ...
- Spring MVC @RequestMapping Annotation Example with Controller, Methods, Headers, Params, @RequestParam, @PathVariable--转载
原文地址: @RequestMapping is one of the most widely used Spring MVC annotation.org.springframework.web.b ...
- Spring MVC中基于注解的 Controller
终于来到了基于注解的 Spring MVC 了.之前我们所讲到的 handler,需要根据 url 并通过 HandlerMapping 来映射出相应的 handler 并调用相应的方法以响 ...
- spring mvc中的service和controller中读取不到properties值
根据web.xml读取配置文件中的顺序来看 controller层和service层来自于spring mvc.xml中读取,所以必须要在spring mvc.xml中配置读取资源文件夹方式
- [Spring MVC]学习笔记--@Controller
在讲解@Controller之前,先说明一下Spring MVC的官方文档在哪. 可能会有人和我一样,在刚接触Spring MVC时,发现在Spring的网站上找不到Spring MVC这个项目. 这 ...
- SpringBoot使用MockMVC单元测试Controller
对模块进行集成测试时,希望能够通过输入URL对Controller进行测试,如果通过启动服务器,建立http client进行测试,这样会使得测试变得很麻烦,比如,启动速度慢,测试验证不方便,依赖网络 ...
- spring mvc通过客户端传值,controller获取Sort对象
之前客户端需要根据需求按不同的排序方式查看数据,按照一种约定排序,比如1代表时间升序,2代表时间降序,3,4这种形式,然后后台根据这些值创建Sort对象. 后来发现完全多此一举,可以根据特定的方式,直 ...
- Spring mvc Controller接口
Spring MVC 1. 继承该接口 Controller接口,重写对应方法,或者采用注解Controller,自定义映射文件 @Controller @RequestMapping("/ ...
- 就是这么简单(续)!使用 RestAssuredMockMvc 测试 Spring MVC Controllers
就是这么简单(续)!使用 RestAssuredMockMvc 测试 Spring MVC Controllers 转载注明出处:http://www.cnblogs.com/wade-xu/p/43 ...
随机推荐
- webpack详细配置解析
阅读本文之前,先看下面这个webpack的配置文件,如果每一项你都懂,那本文能带给你的收获也许就比较有限,你可以快速浏览或直接跳过:如果你和十天前的我一样,对很多选项存在着疑惑,那花一段时间慢慢阅读本 ...
- 第九次Scrum meeting
第九次Scrum meeting 任务及完成度: 成员 12.31 1.1 陈谋 任务1040:完成stackoverflow的数据处理后的json处理(99%) 任务1114-1:完成对网页数据的 ...
- junit-test
一.题目简介: 用单元测试junit4测试calculator类的加减乘除四种方法,来初步学习junit4的学习方法. 二.源码的github链接 :https://github.com/weare ...
- QT 窗口置顶功能
Qt中,保持窗口置顶的设置为: Qt::WindowFlags m_flags = windowFlags(); setWindowFlags(m_flags | Qt::WindowStaysOnT ...
- 如何把数据快速批量添加到Elasticsearch中
问题来源 最近新做一个项目,有部分搜索比较频繁的数据,而且量级比较大,预计一两年时间很可能达到100G,项目要求不要存在数据库中,最终出来有两个方案,一个是使用Protocol Buffers存储在文 ...
- [2017BUAA软工]个人项目
软工个人项目 一.Github项目地址 https://github.com/Lydia-yang/2017BUAA-SoftwareEngineering 二.解题思路 在刚开始拿到题目的时候,关于 ...
- 使用composer遇到的问题及解决方法
可以尝试利用composer下载Yii框架,编辑composer.json文件: { "require":{ "yiisoft/yii2":"~2.0 ...
- Server Tomcat v7.0 Server at libra failed to start
https://stackoverflow.com/questions/13244233/server-tomcat-v7-0-server-at-localhost-failed-to-start- ...
- Node http请求信息
//1:加载http模块 httpconst http = require("http");//2:创建http 服务器var server = http.createServer ...
- 使用 jstack 查询线程死锁错误日志 定位问题
定位问题 (1) 首先 找到相应的进程 使用 ps -ef | grep 'com.sankuai.qcs.regulation.dispatch' 找到进程的ID;==>21980 (2) t ...