spring boot——MockMvc的用法 (SpringBoot 1.5.18)下测试通过
spring boot——MockMvc的用法
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
2.所需对象及Controller
public class User {
private String id;
private String username;
private Date birthday;
public User(String id, String username) {
this.id = id;
this.username = username;
}
//get/set
}
构造一个queryUserList服务
@RestController
@RequestMapping(value="/user")
public class UserController {
@GetMapping
public List<User> queryUserList(){
//使用反射打印出信息
System.out.println(ReflectionToStringBuilder.toString(condition,ToStringStyle.MULTI_LINE_STYLE));
List<User> users = new ArrayList<>();
users.add(new User("1","张三"));
users.add(new User("2","李四"));
users.add(new User("3","王五"));
return users;
}
}
编写mockmvc测试用例
@RunWith(SpringRunner.class)
@SpringBootTest
public class UserControllerTest {
//伪造mvc环境
@Autowired
private WebApplicationContext mac;
private MockMvc mockMvc;
@Before
public void setup() {
mockMvc = MockMvcBuilders.webAppContextSetup(mac).build();
}
@Test
public void whenGenInfoSuccess() throws Exception{
String result = mockMvc.perform(get("/user")
.contentType(MediaType.APPLICATION_JSON_UTF8))
.andExpect(status().isOk())
.andReturn().getResponse().getContentAsString();
System.out.println(result);
}
}
原文地址:http://blog.51cto.com/mazongfei/2334045
spring boot——MockMvc的用法 (SpringBoot 1.5.18)下测试通过的更多相关文章
- spring boot 系列之八:SpringBoot处理定时任务
项目经常会用到定时任务,springboot自然是可以通过整合相关组件来实现的. 目前常用的定时任务的实现有两种: 通过spring 自带的定时器任务@Schedule来实现 通过Quartz来实现 ...
- spring boot 学习(十四)SpringBoot+Redis+SpringSession缓存之实战
SpringBoot + Redis +SpringSession 缓存之实战 前言 前几天,从师兄那儿了解到EhCache是进程内的缓存框架,虽然它已经提供了集群环境下的缓存同步策略,这种同步仍然需 ...
- Spring boot(二) springboot + jsp
官方不推荐JSP在Spring Boot中使用! 一.添加依赖 在pim.xml 里面添加以下 jsp依赖 <dependency> <groupId>org.springfr ...
- spring boot 2.x 系列——spring-boot 集成 Swagger2 打造在线接口文档
文章目录 一.Springfox 与 Swagger 简介 1.1 Springfox 1.2 Swagger 1.3 OpenApi.Swagger.Springfox的关系 二.spring bo ...
- Spring Boot笔记(七) springboot 集成 JavaMail 实现邮箱认证
个人博客网:https://wushaopei.github.io/ (你想要这里多有) 一.JavaMail 1.什么是JavaMail? JavaMail,顾名思义,提供给开发者处理 电子邮 ...
- spring boot 系列之七:SpringBoot整合Mybatis
springboot已经很流行,但是它仍需要搭配一款ORM框架来实现数据的CRUD,之前已经分享过JdbcTemplete和JPA的整合,本次分享下Mybatis的整合. 对于mybatis的使用,需 ...
- spring boot 学习10 定义springboot的两种方法
使用spring boot的两种方法: A:继承spring-boot-starter-parent项目 这种方式很简单,只需要在POM里面添加parent父工程即可. B: 如果你不喜欢继承spri ...
- spring boot使用profile来区分正式环境配置文件与测试环境配置文件
转载请在页首注明作者与出处 一:前言 经常在开发的时候,项目中的配置文件,在个人开发的时候有一套配置文件,在测试环境有一套配置文件,在正式环境有一套配置文件,这个时候如果配置文件复杂,需要改的东西就特 ...
- spring boot 打jar包,获取resource路径下的文件
前言:最近在spring boot项目静态类中获取resource路径下文件,在idea中启动都可以获取,但是打包后变成了jar包 就无法获取到. 我想到了两种方法,一种是根据http访问静态资源比如 ...
随机推荐
- requests 的使用
1.1.实例引入 # 引入Requests库 import requests # 发起GET请求 response = requests.get('https://www.baidu.com/') ...
- nginx与apache 限制ip连接数和带宽方法
今天有个人问我,nginx怎么限制ip连接数,突然想不起来了,年龄大了,脑子不怎么好使了.还要看一下配置才想起了.那个人又问我,你测试过的吗?一下子把我问蒙了,我真没测试过了,也不知道启作用了没有. ...
- CRT Library Features
CRT Library Features The new home for Visual Studio documentation is Visual Studio 2017 Documentatio ...
- php-fpm包的安装与配置
实验环境:CentOS7 [root@~ localhost]#yum -y install php-fpm php-fpm包:用于将php运行于fpm模式 #在安装php-fpm时,一般同时安装如下 ...
- oracle--基础查询(1)
--查询所有列语句 select * from emp; ---查询指定列表的查询语句 select empno,ename,sal from emp; --带有简单算术运算符的简单查询 select ...
- JavaScript语言精粹知识点总结
1.NaN是一个数值,它表示一个不能产生正常结果的运算结果.NaN不等于任何值,包括它自己. 2.Infinity表示所有大于1.79769313486231570e+308的值,所以Infinity ...
- HTML5 中文乱码
<meta charste="utf-8"> 只是告诉浏览器要用utf-8来解释,而文档的编码,是在你保存时的选择决定的.如果保存ANSI 然后用utf-8解释,肯定是 ...
- python使用GUI(图形用户界面)
打开后: File→New File(Ctrl + N)
- Dialog 自定义使用1
一: 布局文件 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:andr ...
- 阶段4-独挡一面\项目-基于视频压缩的实时监控系统\Sprint1-基于Epoll架构的采集端程序框架设计\第2课-基于Epoll的采集端程序框架设计
回顾之前的整个程序架构 把epoll机制应用到这个架构上去 下面主要去分析我们的系统中有没有需要等待的事件,先看看采集子系统 在采集子系统当中,摄像头有数据,摄像头采集到图像数据可以作为一个等待事件. ...