当前位置: 首页 > news >正文

SpringBootTest Mockito 虚实结合编写测试

SpringBootTest & Mockito 虚实结合测试

起因

单一使用mockito,会出现很多mock困难的问题,导致测试编写过程太长,太恶心
单一使用springboottest,会遇到需要外部接口的地方,这个时候就非得去真实调用才行。也很恶心
所以 想到了混合使用 ,这个方法非原创,纯记录,以下的内容都是自己真实的

常用注解

注解使用时机
@MockBean全部都走mock
@SpyBean除特殊指定mock外,都执行真实方法

示例

import cn.hutool.core.util.RandomUtil;
import com.xxxx.util.exception.ServiceException;
import com.xxxx.xxx.common.core.entity.user.xxxxConfig;
import com.xxxx.xxx.common.core.utils.SecurityUtils;
import com.xxxx.xxx.common.mybatis.mapper.userMapper;
import com.xxxx.xxx.user.dto.xxxxDTO;
import com.xxxx.xxx.user.service.xxxxConfigService;
import com.xxxx.xxx.user.vo.xxxxVO;
import com.xxxx.xxx.verify.code.service.xxxxService;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.annotation.Rollback;
import org.springframework.transaction.annotation.Transactional;import javax.annotation.Resource;@Transactional
@SpringBootTest
@Rollback
// 当模块中存在websocket的时候,需要使用下方注解配置,方可启动成功(以下配置会启动服务)
// @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class XxxxConfigServiceImplTest {@Resourceprivate XxxxConfigService xxxxConfigService;@MockBean(name = "userMapper")private UserMapper myUserMapper;@Resourceprivate XxxxService xxxxService;public static final String ACCOUNT = RandomUtil.randomString(8);public static final String TEL = RandomUtil.randomNumbers(11);@BeforeEachvoid init() {// mock方法返回Mockito.when(myUserMapper.selectTelByAccount(Mockito.anyString())).thenReturn(TEL);}@Test@DisplayName("修改:成功")void update() {// 以下都是执行真实代码xxxxDTO xxDTO = new xxxxDTO();xxDTO.setAccount(ACCOUNT);xxDTO.setPassword("123456");xxDTO.setStartTime("00:00");xxDTO.setEndTime("23:59");xxDTO.setCaptchaCode("0000");xxxxConfigService.sendCode(ACCOUNT);xxxxConfigService.update(xxDTO);xxxxConfig controlConfig = xxxxConfigService.lambdaQuery().eq(xxxxConfig::getAccount, ACCOUNT).one();assert controlConfig.getAccount().equals(xxDTO.getAccount());assert controlConfig.getStartTime().equals(xxDTO.getStartTime());assert controlConfig.getEndTime().equals(xxDTO.getEndTime());}
}

常见问题

  • MockBean导致启动失败,提示 org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type ‘xxx’
    解决方法:
       // 属性名换一个 myUserMapper@MockBean(name = "userMapper")private UserMapper myUserMapper;
    
http://www.lryc.cn/news/456204.html

相关文章:

  • 国内外网络安全政策动态(2024年9月)
  • 使用Android studio进行Unit Test中遇到的问题记录
  • 智能运维与问题诊断工具:提升生产环境的安全稳定性
  • 【MAUI】CommunityToolkit社区工具包介绍
  • 【答疑解惑】图文深入详解undo和redo的区别及其底层逻辑
  • 低通滤波、反相放大器电路
  • SpringBoot助力服装生产流程优化
  • 【机器学习】线性回归算法简介 及 数学实现方法
  • 设计模式的学习
  • wordpress发邮件SMTP服务器配置步骤指南?
  • 胤娲科技:机械臂「叛逃」记——自由游走,再悄然合体
  • 分布式事务讲解 - 2PC、3PC、TCC
  • 前端基础面试题·第四篇——Vue(其二)
  • PHP反射
  • Gated Transformer Networks for Multivariate Time Series Classification
  • 这一次,AI真的能帮你实现职场跃迁
  • Python绘制--绘制心形曲线
  • 初识Linux · 自主Shell编写
  • 海报设计模板免费的好用吗?活动海报排版技巧轻松get
  • 【Linux基础】03 Linux环境基础开发工具使用
  • 【CSS】flex: 1; 的意思
  • C++ 3D冒险游戏开发案例
  • 【AIGC】Exa AI 要做 AI 领域的 Google
  • YOLOv8 基于MGD的知识蒸馏
  • 全国消防知识竞赛活动方案哪家强
  • JavaEE学习一条龙服务————概述
  • 分支预测器BPU
  • Go 系列教程 —— 数组和切片
  • 适配器模式【对象适配器模式和类适配器模式,以及具体使用场景】
  • 【EXCEL数据处理】保姆级教程 000016案例 EXCEL的vlookup函数。