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; ...
随机推荐
- oracle数据库锁表
在团队开发一个项目的时候,避免不了两个或两个以上的人同时操作某一数据库中的同一张表,这时候,如果一个用户没有提交事务,或者忘记提交事务,那么其他用户就不能对这张表进行操作了,这是很烦人的事情,下面是查 ...
- RCNN,Fast RCNN,Faster RCNN 的前生今世:(3) SPP - Net
SPP-Net是出自2015年发表在IEEE上的论文-<Spatial Pyramid Pooling in Deep ConvolutionalNetworks for Visual Reco ...
- Spark 的两种核心 Shuffle (HashShuffle 与 与 SortShuffle) 的 的工作流程
1. 参考博客:https://blog.csdn.net/qichangjian/article/details/88039576
- mov指令和 add以及sub 指令的区别
比如 mov ax,ds 比如 [0],ds #经过上机实验 add,sub指令不能对段寄存器操作
- Linux运维:安装CentOS7图解
Ago linux运维群: 93324526 笔者QQ:578843228 此篇博文针对最小化安装,和只有图解.有不懂地方,欢迎加群询问. 此篇以CentOS7.2为例
- STP生成树详解图
- OSPF外部实验详解
- DM中将有缝隙的面体缝合为实体
原版视频下载地址链接: https://pan.baidu.com/s/1mi0NOeO 密码: nw7g
- FCN笔记
FCN.py tensorflow命令行参数 FLAGS = tf.flags.FLAGS tf.flags.DEFINE_integer("batch_size", " ...
- ssh端口映射总结
版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/boliang319/article/det ...