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

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 {
    
    @Resource
    private XxxxConfigService xxxxConfigService;
    
    @MockBean(name = "userMapper")
    private UserMapper myUserMapper;
    
    @Resource
    private XxxxService xxxxService;
    
    public static final String ACCOUNT = RandomUtil.randomString(8);
    public static final String TEL = RandomUtil.randomNumbers(11);
    
    @BeforeEach
    void 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.kler.cn/news/327544.html

相关文章:

  • LPDDR4芯片学习(二)——Functional Description
  • 解锁高效开发的秘密武器
  • <Rust>egui学习之部件(十三):如何为文本添加链接Link和超链接HyperLink功能?
  • 基于J2EE技术的高校社团综合服务系统
  • 设计模式---代理模式
  • webpack使用
  • 【Spring Boot 入门一】构建你的第一个Spring Boot应用
  • ECMAScript 详解
  • 新版本Android Studio如何新建Java code工程
  • MATLAB中数据导入与导出的全面指南
  • Ionic 颜色:探索前端开发的调色板
  • Spring Boot实现足球青训俱乐部管理自动化
  • ubuntu20.04系统安装zookeeper简单教程
  • Paddlets时间序列集成模型回测实战:MLPRegressor、NHiTSModel与RNNBlockRegressor
  • # VirtualBox中安装的CentOS 6.5网络设置为NAT模式时,怎么使用SecureCRT连接CentOS6.5系统?
  • 计算机毕业设计 基于Python的广东旅游数据分析系统的设计与实现 Python+Django+Vue Python爬虫 附源码 讲解 文档
  • WPF中的switch选择
  • Visual Studio-X64汇编编写
  • stm32单片机学习 - MDK仿真调试
  • Redis篇(缓存机制 - 多级缓存)(持续更新迭代)
  • MySQL—表优化
  • 平衡二叉搜索树删除的实现
  • Spring Cloud全解析:服务调用之OpenFeign集成OkHttp
  • 一次阿里云ECS免费试用实践
  • leetcode-链表篇4
  • MATLAB编写的RSSI在三维空间上的定位程序,锚点数量无限制(可自定义),带中文注释
  • 如何获取钉钉webhook
  • docker容器mysql数据备份 mysql容器无法启动备份数据
  • 【docker学习】Linux系统离线方式安装docker环境方法
  • 【Linux系列】CMA (Contiguous Memory Allocator) 简单介绍