这期内容当中小编将会给大家带来有关如何弹簧引导中使用MockMvc对象进行单元测试,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。
春天测试框架提供MockMvc对象,可以在不需要客户端——服务端请求的情况下进行MVC测试,完全在服务端这边就可以执行控制器的请求,跟启动了测试服务器一样。
测试开始之前需要建立测试环境,设置方法被@Before修饰。通过MockMvcBuilders工具,使用WebApplicationContext对象作为参数,创建一个MockMvc对象。
MockMvc对象提供一组工具函数用来执行断言判断,都是针对web请求的判断。这组工具的使用方式是函数的链式调用,允许程序员将多个测试用例链接在一起,并进行多个判断。在这个例子中我们用到下面的一些工具函数:
执行(get(…))建立网络请求。在我们的第三个用例中,通过MockMvcRequestBuilder执行得到请求。
一下动身(…)可以在执行(…)函数调用后多次调用,表示对多个条件的判断,这个函数的参数类型是ResultMatcher接口,在MockMvcResultMatchers这这个类中提供了很多返回ResultMatcher接口的工具函数。这个函数使得可以检测同一个web请求的多个方面,包括HTTP响应状态码(响应状态),响应的内容类型(内容类型),会话中存放的值,检验重定向,模型或者头的内容等等。这里需要通过第三方库json-path检测JSON格式的响应数据:检查JSON数据包含正确的元素类型和对应的值,例如jsonPath (“.name"美元)value(“中文测试“)用于检查在根目录下有一个名为名字的节点,并且该节点对应的值是“testuser”。
本文对rest api的开发不做详细描述,如需了解可以参考弹簧引导实战之其他接口开发及数据库基本操作
1,修改砰的一声。xml,添加依赖库json-path,用于检测JSON格式的响应数据
& lt; dependency>, & lt;才能groupId> com.jayway.jsonpath</groupId>, & lt;才能artifactId> json-path</artifactId>, & lt;/dependency>
, 2,添加用户数据模型用户信息。java
package com.xiaofangtech.sunt.bean;, , import javax.persistence.Entity, import javax.persistence.GeneratedValue, import javax.persistence.GenerationType, import javax.persistence.Id, import javax.persistence.Table, import javax.validation.constraints.Size, , @Entity @ table (name=皌_userinfo"), {public class  UserInfo ,,@Id @GeneratedValue才能(strategy =, GenerationType.AUTO),, private 才能Long id, @才能(min=0,, max=32), private 才能;String 名字,, ,, private 才能;Integer 年龄;, @才能(min=0, max=255), private 才能;String 地址,, , public 才能;Long  getId (), {, ,,,return id,, ,,}, , public 才能;void  setId (Long id), {, ,,,this.id =, id,, ,,}, , public 才能;String  getName (), {, ,,,return 名字,, ,,}, , public 才能;void  setName (String 名称),{, ,,,this.name =,名字,, ,,}, , public 才能;Integer  getAge (), {, ,,,return 年龄;, ,,}, , public 才能;void  setAge (Integer 年龄),{, ,,,this.age =,年龄,, ,,}, , public 才能;String  getAddress (), {, ,,,return 地址,, ,,}, , public 才能;void  setAddress (String 地址),{, ,,,this.address =,地址,, ,,}, }
3,添加控制器用户控件。java,用于实现对用户的增删改查
package com.xiaofangtech.sunt.controller;, , import 并不知道, , import org.springframework.beans.factory.annotation.Autowired, import org.springframework.data.jpa.repository.Modifying, import org.springframework.web.bind.annotation.RequestBody, import org.springframework.web.bind.annotation.RequestMapping, import org.springframework.web.bind.annotation.RequestMethod, import org.springframework.web.bind.annotation.RestController, , import com.xiaofangtech.sunt.bean.UserInfo, import com.xiaofangtech.sunt.repository.UserInfoRepository, import com.xiaofangtech.sunt.utils。*, , @RestController , @RequestMapping (“user"), {public class  UserController ,,@Autowired private 才能;UserInfoRepository  userRepositoy;, ,,/* * *,才能 ,,*,根据用户id,获取用户信息, ,,*,@param id ,,*,@return ,,*/, @RequestMapping才能(value=https://www.yisu.com/zixun/癵etuser方法=RequestMethod.GET) 公共对象getUser (id) { 用户信息userEntity=userRepositoy.findOne (id); ResultMsg ResultMsg=new ResultMsg (ResultStatusCode.OK.getErrcode (), ResultStatusCode.OK.getErrmsg (), userEntity); 返回resultMsg; }/* * * *获取所有用户列表 * @return */@RequestMapping (value=癵etalluser”方法=RequestMethod.GET) 公共对象getUserList () {如何弹簧引导中使用MockMvc对象进行单元测试