功能实现之后,我们要养成随手写配套单元测试的习惯,这在微服务架构中尤为重要。通常,我们实施微服务架构的时候,已经实现了前后端分离的项目与架构部署。那么在实现后端服务的时候,单元测试是在开发过程中用来验证代码正确性非常好的手段,并且这些单元测试将会很好地支持我们未来可能会进行的重构。

  编写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的更多相关文章

  1. Spring boot Jpa添加对象字段使用数据库默认值

    Spring boot Jpa添加对象字段使用数据库默认值 jpa做持久层框架,项目中数据库字段有默认值和非空约束,这样在保存对象是必须保存一个完整的对象,但在开发中我们往往只是先保存部分特殊的字段其 ...

  2. spring cloud spring boot JPA 克隆对象修改属性后 无法正常的执行save方法进行保存或者更新

    2019-12-1220:34:58 spring cloud spring boot JPA 克隆对象修改属性后 无法正常的执行save方法进行保存或者更新 未解决

  3. spring boot(三)Junit 测试controller

    Junit测试Controller(MockMVC使用),传输@RequestBody数据解决办法 一.单元测试的目的 简单来说就是在我们增加或者改动一些代码以后对所有逻辑的一个检测,尤其是在我们后期 ...

  4. Spring Boot 异步请求和异步调用,一文搞定

    一.Spring Boot中异步请求的使用 1.异步请求与同步请求 特点: 可以先释放容器分配给请求的线程与相关资源,减轻系统负担,释放了容器所分配线程的请求,其响应将被延后,可以在耗时处理完成(例如 ...

  5. 基于Spring Boot,使用JPA动态调用Sql查询数据

    在<基于Spring Boot,使用JPA操作Sql Server数据库完成CRUD>,<基于Spring Boot,使用JPA调用Sql Server数据库的存储过程并返回记录集合 ...

  6. Spring Boot使用@Async实现异步调用

    原文:http://blog.csdn.net/a286352250/article/details/53157822 项目GitHub地址 : https://github.com/FrameRes ...

  7. spring boot通过@Bean注解定义一个Controller

    功能需求 提供一个公共的jar包给其他业务模块依赖,需要在这个公共的jar中暴露一个restful API 采用spring auto config机制,在公共jar包中定义spring.factor ...

  8. Spring Boot 实现看门狗功能 (调用 Shell 脚本)

    需要实现看门狗功能,定时检测另外一个程序是否在运行,使用 crontab 仅可以实现检测程序是否正在运行,无法做到扩展,如:手动重启.程序升级(如果只需要实现自动升级功能可以使用 inotify)等功 ...

  9. [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; ...

随机推荐

  1. 关于Spring的常用注解和接口

    接口 1. BeanPostProcessor 2. FactoryBean 3. Condition 4. ImportSelector 5. ImportBeanDefinitionRegitra ...

  2. CString、char*l类型转换

    环境:VS2010 1.CString to char* 第一种方法: 需头文件:atlconv.h CString   host_string; //CString to char* USES_CO ...

  3. java中split函数参数特殊字符的处理(转义),如:"." 、"\"、"|"

    内容介绍 本文主要介绍java中特殊字符做为split函数的参数,如:"." ."\"."|",双引号等,不能正确分隔源字符串的处理方法. ...

  4. HDU - 4507 - 吉哥系列故事——恨7不成妻(数位DP,数学)

    链接: https://vjudge.net/problem/HDU-4507 题意: 单身! 依然单身! 吉哥依然单身! DS级码农吉哥依然单身! 所以,他生平最恨情人节,不管是214还是77,他都 ...

  5. usa单位换算

    1.温度换算 摄氏度    C = 5/9(F-32) ≍ (F-32)/1.8 (F为华氏温度值) 华氏度   F = 1.8C + 32 (C为摄氏温度值) 3.重量换算 1品脱(pint) ≍ ...

  6. MAC OSX下终端通过NTLM验证,通过代理上网(花了一天时间才解决这个)

    MAC OSX下终端通过NTLM验证,通过代理上网 公司网络限制如下: 公司通过代理来控制内网用户访问外网的权限.用户名和密码为域用户,采用的验证方式是NTLM(用的是foreFront TMG) 遇 ...

  7. c# 隐藏窗口在ALT+TAB中

    winform: protected override CreateParams CreateParams { get { const int WS_EX_APPWINDOW = 0x40000; c ...

  8. composer 发布一个自己的扩展包

    练习写了一个扩展包,步骤参考github项目的readme.md 文件.https://github.com/codingmonkey9/test 说明:只是自己简单记录一下,没有详细写. 如何发布具 ...

  9. DEV C++的使用

    1.点击dev图标: 2.左上角点击:文件——新建——源代码(快捷键ctrl+N): 3. 然后开始写代码: 4.点击运行: 右边的那个编译加运行(点这个),左边编译,中间运行. 5.保存(可以修改保 ...

  10. Dominating Patterns (AC 自动鸡模版题, 出现次数最多的子串)

    传送门 题意: 给你n个模式串, 再给你一个 文本串,问模式串在文本串中出现次数最多是多少. 出现次数最多的模式串有哪些. 解: 模版题. #include <bits/stdc++.h> ...