Spring Boot 使用MockMvc对象模拟调用Controller
功能实现之后,我们要养成随手写配套单元测试的习惯,这在微服务架构中尤为重要。通常,我们实施微服务架构的时候,已经实现了前后端分离的项目与架构部署。那么在实现后端服务的时候,单元测试是在开发过程中用来验证代码正确性非常好的手段,并且这些单元测试将会很好地支持我们未来可能会进行的重构。
编写controller
package com.xc.springboot.controller; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; @RestController
public class HelloController { /**
* http://127.0.0.1:8081/hello
*/
@RequestMapping("/hello")
public String hello() {
return "hello world!";
} }
src/test/下编写测试类
package com.xc.springboot.controller; import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
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.setup.MockMvcBuilders; //引入下面的静态引用,让status、content、equalTo函数可用
import static org.hamcrest.Matchers.equalTo;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; @RunWith(SpringJUnit4ClassRunner.class)//引入Spring对JUnit4的支持。
// @SpringApplicationConfiguration(classes = SpringbootApplication.class)//指定Spring Boot的启动类。
@WebAppConfiguration//开启Web应用的配置,用于模拟ServletContext。
public class HelloControllerTest { private MockMvc mvc; //·@Before:JUnit中定义在测试用例@Test内容执行前预加载的内容,这里用来初始化对HelloController的模拟。
@Before
public void setUp() throws Exception {
mvc = MockMvcBuilders.standaloneSetup(new HelloController()).build();
} @Test
public void hello() throws Exception { //·MockMvc对象:用于模拟调用Controller的接口发起请求,在@Test定义的hello测试用例中,perform函数执行一次请求调用,accept用于执行接收的数据类型,andExpect用于判断接口返回的期望值。
mvc.perform(MockMvcRequestBuilders.get("/hello").accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().string(equalTo("hello world!")));
} }
代码解析如下。
·@RunWith(SpringJUnit4ClassRunner.class):引入Spring对JUnit4的支持。
·@SpringApplicationConfiguration(classes =SpringbootApplication.class):指定Spring Boot的启动类。
·@WebAppConfiguration:开启Web应用的配置,用于模拟ServletContext。
·MockMvc对象:用于模拟调用Controller的接口发起请求,在@Test定义的hello测试用例中,perform函数执行一次请求调用,accept用于执行接收的数据类型,andExpect用于判断接口返回的期望值。
·@Before:JUnit中定义在测试用例@Test内容执行前预加载的内容,这里用来初始化对HelloController的模拟。
注意引入下面的静态引用,让status、content、equalTo函数可用:
import static org.hamcrest.Matchers.equalTo;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
版本:spring-boot-starter-parent 1.5.21.RELEASE
参考:Spring Cloud微服务实战 p51
Spring Boot 使用MockMvc对象模拟调用Controller的更多相关文章
- Spring boot Jpa添加对象字段使用数据库默认值
Spring boot Jpa添加对象字段使用数据库默认值 jpa做持久层框架,项目中数据库字段有默认值和非空约束,这样在保存对象是必须保存一个完整的对象,但在开发中我们往往只是先保存部分特殊的字段其 ...
- spring cloud spring boot JPA 克隆对象修改属性后 无法正常的执行save方法进行保存或者更新
2019-12-1220:34:58 spring cloud spring boot JPA 克隆对象修改属性后 无法正常的执行save方法进行保存或者更新 未解决
- spring boot(三)Junit 测试controller
Junit测试Controller(MockMVC使用),传输@RequestBody数据解决办法 一.单元测试的目的 简单来说就是在我们增加或者改动一些代码以后对所有逻辑的一个检测,尤其是在我们后期 ...
- Spring Boot 异步请求和异步调用,一文搞定
一.Spring Boot中异步请求的使用 1.异步请求与同步请求 特点: 可以先释放容器分配给请求的线程与相关资源,减轻系统负担,释放了容器所分配线程的请求,其响应将被延后,可以在耗时处理完成(例如 ...
- 基于Spring Boot,使用JPA动态调用Sql查询数据
在<基于Spring Boot,使用JPA操作Sql Server数据库完成CRUD>,<基于Spring Boot,使用JPA调用Sql Server数据库的存储过程并返回记录集合 ...
- Spring Boot使用@Async实现异步调用
原文:http://blog.csdn.net/a286352250/article/details/53157822 项目GitHub地址 : https://github.com/FrameRes ...
- spring boot通过@Bean注解定义一个Controller
功能需求 提供一个公共的jar包给其他业务模块依赖,需要在这个公共的jar中暴露一个restful API 采用spring auto config机制,在公共jar包中定义spring.factor ...
- Spring Boot 实现看门狗功能 (调用 Shell 脚本)
需要实现看门狗功能,定时检测另外一个程序是否在运行,使用 crontab 仅可以实现检测程序是否正在运行,无法做到扩展,如:手动重启.程序升级(如果只需要实现自动升级功能可以使用 inotify)等功 ...
- [Spring Boot ] Creating the Spring Boot Project : Demo: Creating a REST Controller
In Spring boot, define a REST API endpoint is pretty easy. package com.globomatisc.bike.controllers; ...
随机推荐
- 【转载】Mixed mode assembly is built against version 'v2.0.50727' of the runtime and cannot be loaded in the 4.0 runtime without additional configuration info
网上查到http://social.msdn.microsoft.com/Forums/vstudio/en-US/58271e39-beca-49ac-90f9-e116fa3dd3c0/mxed- ...
- Uoj #35. 后缀排序(后缀数组)
35. 后缀排序 统计 描述 提交 自定义测试 这是一道模板题. 读入一个长度为 nn 的由小写英文字母组成的字符串,请把这个字符串的所有非空后缀按字典序从小到大排序,然后按顺序输出后缀的第一个字符在 ...
- Processing 中玩增强现实 Argument Reality
其实2009年Processing就能做AR了,只是我不知道而已~ 需要以下几个东西: 1.JMyron 2.GSVideo 3.nyar4psg 4.Picking 5.OBJLoader 或者大伙 ...
- Mybatis 通用Mapper增强
1.确保是个Maven项目,确保Spring与Mybatis正确配置. 2.新建一个自定义通用Mapper. /** * BaseMapper接口:使mapper包含完整的CRUD方法<br&g ...
- 菜鸟的算法入门:java的链表操作
从C语言的指针开始,我的算法之路就结束了! 今天为了找个好的实习,不得不捡起来,写了三年的web,算法落下了太多了 今天在leetcode上刷题,难在了一个简单的链表上,因此记录一下 题目:给定两个非 ...
- 使用Android的日志工具Log
Android中的日志工具类是Log,这个类中提供了5个方法来供我们打印日志 1.Log.v()用于打印那些最为琐碎的,意义最小的日志信息.对应级别verbose,是Android日志里面级别最低的一 ...
- 部署owa预览服务
b部署owa预览服务 一.前言 1.1 服务器需求 转换文档需要两台服务器,一台为转换server,另外一台为域控server.(安装office web apps的服务器必须加域才能安装成功,如果没 ...
- 【2019.10.17】十天Web前端程序员体验(软件工程实践第五次作业)
结对信息.具体分工 Github地址:https://github.com/MokouTyan/131700101-031702425 学号 昵称 主要负责内容 博客地址 131700101 莫多 代 ...
- kubernetes(K8S)集群及Dashboard安装配置
环境准备 机器信息 主机名 操作系统 IP地址 K8sm-218 Centos 7.5-x86_64 172.17.0.218 k8s-219 Centos 7.5-x86_64 172.17.0.2 ...
- CAT中实现异步请求的调用链查看
CAT简介 CAT(Central Application Tracking),是美团点评基于 Java 开发的一套开源的分布式实时监控系统.美团点评基础架构部希望在基础存储.高性能通信.大规模在线访 ...