java结合测试的步骤_Mockito 结合 Springboot 进行应用测试的方法详解

Spring Boot可以和大部分流行的测试框架协同工作:通过Spring JUnit创建单元测试;生成测试数据初始化数据库用于测试;Spring Boot可以跟BDD(Behavier Driven Development)工具、Cucumber和Spock协同工作,对应用程序进行测试。

在web应用程序中,我们主要是对Service层做单元测试,以前单元测试都是使用 junit4 ,对Controller层做集成测试或者接口测试,对Controller层的测试一般有两种方法:(1)发送http请求;(2)模拟http请求对象。

第一种方法需要配置回归环境,通过修改代码统计的策略来计算覆盖率;第二种方法是比较正规的思路。

Mockito网上相关的文档不是很多,基本都是入门性质的没有更深层次的使用案例,而且Mockito本身功能也在不断的完善,导致写起来比较费劲,好多地方完全靠猜。摸索之下算是完成了,把踩过的坑记录一下,万一有人需要呢。

下面我将演示如何用Mock对象测试Service、Controller层的代码。

引入相关jar

org.springframework.boot

spring-boot-starter-web

org.springframework.boot

spring-boot-starter-test

test

项目使用的是 springboot2.4.0。

spring-boot-starter-test 中包含 junit5 和Mockito 相关jar。无需额外引入。

如果想使用 junit4,可以将springboot版本降低,junit4 与 junit5 在一些注解和方法上有区别,比如注解的引入目录不同,一些方法进行了优化,有兴趣可以查阅相关资料,这里就不再赘述。

下面代码是 junit5 使用样式。

项目目录结构如下

a9e13d6790d8ffeeffd83d375ba908a9.png

Controller类

@RestController

@RequestMapping("/api/v1")

public class UserController {

@Autowired

UserService userService;

@GetMapping("user/{userId}")

public User say(@PathVariable("userId") Long id) {

return userService.getUser(id);

}

@PostMapping("user/edit")

public User edit(@RequestBody User user) {

return userService.edit(user);

}

}

Service 实现类

@Service

public class UserServiceImpl implements UserService {

@Autowired

UserDao userDao;

@Override

public User getUser(Long id) {

return userDao.getUser(id);

}

@Override

public User edit(User user) {

return userDao.edit(user);

}

}

Dao 接口

public interface UserDao {

User getUser(Long id);

User edit(User user);

}

User 类

public class User {

private Long id;

private String name; private String desc;

get()... set()... toString()...

}

UserDao 是一个接口,没有任何的相关实现。所以对该接口进行mock。测试代码如下

package com.mmling.mockitodemo;import com.mmling.mockitodemo.controller.UserController;import com.mmling.mockitodemo.dao.UserDao;import com.mmling.mockitodemo.entity.User;import com.mmling.mockitodemo.service.UserService;import org.junit.jupiter.api.BeforeEach;import org.junit.jupiter.api.Test;import org.junit.jupiter.api.extension.ExtendWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.boot.test.mock.mockito.MockBean;import org.springframework.http.MediaType;import org.springframework.test.context.junit.jupiter.SpringExtension;import org.springframework.test.web.servlet.MockMvc;import org.springframework.test.web.servlet.ResultActions;import org.springframework.test.web.servlet.setup.MockMvcBuilders;import static org.mockito.ArgumentMatchers.any;import static org.mockito.ArgumentMatchers.anyLong;import static org.mockito.Mockito.times;import static org.mockito.Mockito.verify;import static org.mockito.Mockito.when;import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;/** * @author Robert * @date 2020-11-27 14:38 */@ExtendWith(SpringExtension.class)@SpringBootTest(classes = MockitoDemoApplication.class)public class UserBeanTest { @Autowired UserController controller; @Autowired UserService userService; @MockBean //需要mock的bean,会自动注入到调用的对象中 private UserDao userDao; MockMvc mockMvc; /** * 测试 service 层 */ @Test public void test() { // 定义未实现的 service 返回 when(userDao.getUser(anyLong())).thenReturn(new User(anyLong(), "张三", "路人")); System.out.println(userService.getUser(12L).toString()); verify(userDao, times(1)).getUser(anyLong()); } /** * 测试 controller 时,需要构建 mvc 环境 */ @BeforeEach public void setup() { //构建mvc环境 mockMvc = MockMvcBuilders.standaloneSetup(controller).build(); } /** * .perform() : 执行一个MockMvcRequestBuilders的请求;MockMvcRequestBuilders有.get()、.post()、.put()、.delete()等请求。 * .andDo() : 添加一个MockMvcResultHandlers结果处理器,可以用于打印结果输出(MockMvcResultHandlers.print())。 * .andExpect : 添加MockMvcResultMatchers验证规则,验证执行结果是否正确。 */ @Test public void testGetUser() throws Exception { // 定义未实现的 service 返回 when(userDao.getUser(anyLong())).thenReturn(new User(12L, "张三", "路人")); //模拟接口调用 ResultActions perform = this.mockMvc.perform(get("/api/v1/user/12")); //对接口响应进行验证 perform.andExpect(status().isOk()) .andExpect(content().json("{id:12,name:张三,desc:路人}")); // 可以不用写成转义后的json格式 System.out.println(perform.andReturn().getResponse().getContentAsString()); } @Test public void testEditUser() throws Exception { // 定义未实现的 service 返回 when(userDao.edit(any(User.class))).thenReturn(new User(12L, "张三", "路人")); //模拟接口调用 ResultActions perform = this.mockMvc.perform(post("/api/v1/user/edit") .contentType(MediaType.APPLICATION_JSON) .content("{\"id\":12,\"name\":\"张三\",\"desc\":\"路人\"}")); // 必须写成转义后的json格式,否则没法转换 //对接口响应进行验证 perform.andExpect(status().isOk()) .andExpect(content().json("{id:12,name:张三,desc:路人}")); // 可以不用写成转义后的json格式 System.out.println(perform.andReturn().getResponse().getContentAsString()); }}

注意:

1.由于这是Spring Boot的测试,因此我们可通过@Autowired注解织入任何由Spring管理的对象,或者是通过@Value设置指定的环境变量的值。

2.每个测试用例用@Test注解修饰。

3.第一个测试用中展示了如何测试 Service 层代码

4.第二个第三个测试用例中展示了如何通过MockMvc对象实现对RESTful URL接口订单查询的测试。Spring测试框架提供MockMvc对象,可以在不需要客户端-服务端请求的情况下进行MVC测试,完全在服务端这边就可以执行Controller的请求,跟启动了测试服务器一样。

5.测试开始之前需要建立测试环境,setup方法被@Before修饰。通过MockMvcBuilders工具,使用 controller 对象作为参数,创建一个MockMvc对象。

6.mockMvc 可以链式调用,进行接口调用,并判断状态

//模拟接口调用

ResultActions perform = this.mockMvc.perform(get("/api/v1/user/12"))

.andExpect(status().isOk())

.andExpect(content().json("{id:12,name:张三,desc:路人}")); // 可以不用写成转义后的json格式

7. content().json() 会对结果进行处理,所以判断的无需转义,但this.mockMvc.perform(post("/api/v1/user/edit").contentType(MediaType.APPLICATION_JSON).content() 中的json是需要手动转义的。

到此这篇关于Mockito 结合 Springboot 进行应用测试的方法详解的文章就介绍到这了,更多相关Mockito 结合 Springboot应用测试内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
【优质项目推荐】 1、项目代码均经过严格本地测试,运行OK,确保功能稳定后才上传平台。可放心下载并立即投入使用,若遇到任何使用问题,随时欢迎私信反馈与沟通,博主会第一时间回复。 2、项目适用于计算机相关专业(如计科、信息安全、数据科学、人工智能、通信、物联网、自动化、电子信息等)的在校学生、专业教师,或企业员工,小白入门等都适用。 3、该项目不仅具有很高的学习借鉴价值,对于初学者来说,也是入门进阶的绝佳选择;当然也可以直接用于 毕设、课设、期末大作业或项目初期立项演示等。 3、开放创新:如果您有一定基础,且热爱探索钻研,可以在此代码基础上二次开发,进行修改、扩展,创造出属于自己的独特应用。 欢迎下载使用优质资源!欢迎借鉴使用,并欢迎学习交流,共同探索编程的无穷魅力! 基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip 基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip 基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值