博客
关于我
mock静态方法指引
阅读量:800 次
发布时间:2023-02-09

本文共 3144 字,大约阅读时间需要 10 分钟。

mock静态方法指引

mockito 在3.4.0版本开始支持mock static method

文档:https://wttech.blog/blog/2020/mocking-static-methods-made-possible-in-mockito-3.4.0/

1. 升级Maven依赖

org.mockito
mockito-core
3.6.28
test
org.mockito
mockito-inline
3.6.28
test
org.mockito
mockito-junit-jupiter
3.4.0
test

2. 使用方式

  • 静态方法
public final class MysteryBox {       public static Optional
amaze(String codeWord) { // todo }}
  • 待测试的类,使用了MysteryBox.amaze(String)静态方法
public class HaplessUser {     public Mystery unleashMystery(String magicSpell) {       Optional
om = MysteryBox.amaze(magicSpell); return om.orElseThrow(() -> new FailureToAmazeException("The box was empty")); }}
  • 单元测试类,mock静态方法
@Test@DisplayName("Should throw an exception upon failing to uncover mind-boggling mysteries")void testUncoverMysteries() {     // 1.在try代码块中实例化一个MockedStatic,使用范围仅仅在try代码块内  try (MockedStatic
mb = Mockito.mockStatic(MysteryBox.class)) { // 2. mock静态方法的调用 mb.when(() -> { MysteryBox.amaze(any(String.class )) }) .thenReturn(Optional.empty()); // 3. 调用单元测试方法 assertThrows(FailureToAmazeException.class, () -> subjectUnderTest.unleashMystery("Abracadabra")); } // 静态方法的mock在这里是不可用的}

3. 异常情况

在同个测试类,当多个测试方法都需要mock调用某个静态方法时,代码如下

@ExtendWith(MockitoExtension.class)public class KonfigurationCopyServiceTest {     @InjectMocks	private EKonfigurationCopyServiceImpl konfigurationCopyServiceImpl;		@MockBean	private FileProcessRecordServiceImpl fileProcessRecordService;  @BeforeEach  public void setUp() {       // mock静态方法		MockedStatic
mockSpringUtil = Mockito.mockStatic(SpringUtil.class); mockSpringUtil.when(() -> SpringUtil.getActiveProfile()).thenReturn("dev"); } @Test void test1() { // 省略@Test的单元测试方法 } @Test void test2() { // 省略@Test的单元测试方法 }}

此时,会抛出如下异常信息:

org.mockito.exceptions.base.MockitoException: For xx.xxxx.util.SpringUtil, static mocking is already registered in the current threadTo create a new mock, the existing static mock registration must be deregistered

静态mock已经在当前线程中注册了,要创建新的mock,必须注销现有的静态mock注册

解决方式
  • 导入
import static org.junit.jupiter.api.TestInstance.Lifecycle.PER_CLASS;
  • 在测试类上添加注解@TestInstance(PER_CLASS)
  • 使用@BeforeAll代替@BeforeEach
// 添加@TestInstance(PER_CLASS)注解@TestInstance(PER_CLASS)@ExtendWith(MockitoExtension.class)public class KonfigurationCopyServiceTest {     @InjectMocks	private EKonfigurationCopyServiceImpl konfigurationCopyServiceImpl;		@MockBean	private FileProcessRecordServiceImpl fileProcessRecordService;  // 使用@BeforeAll mock静态方法  @BeforeAll  public void setUp() {       // mock静态方法		MockedStatic
mockSpringUtil = Mockito.mockStatic(SpringUtil.class); mockSpringUtil.when(() -> SpringUtil.getActiveProfile()).thenReturn("dev"); } @BeforeEach public void init() { // mock其他方法 } @Test void test1() { // 省略@Test的单元测试方法 } @Test void test2() { // 省略@Test的单元测试方法 }}

转载地址:http://fbffk.baihongyu.com/

你可能感兴趣的文章
Mysql5.7深入学习 1.MySQL 5.7 中的新增功能
查看>>
Webpack 之 basic chunk graph
查看>>
Mysql5.7版本单机版my.cnf配置文件
查看>>
mysql5.7的安装和Navicat的安装
查看>>
mysql5.7示例数据库_Linux MySQL5.7多实例数据库配置
查看>>
Mysql8 数据库安装及主从配置 | Spring Cloud 2
查看>>
mysql8 配置文件配置group 问题 sql语句group不能使用报错解决 mysql8.X版本的my.cnf配置文件 my.cnf文件 能够使用的my.cnf配置文件
查看>>
MySQL8.0.29启动报错Different lower_case_table_names settings for server (‘0‘) and data dictionary (‘1‘)
查看>>
MYSQL8.0以上忘记root密码
查看>>
Mysql8.0以上重置初始密码的方法
查看>>
mysql8.0新特性-自增变量的持久化
查看>>
Mysql8.0注意url变更写法
查看>>
Mysql8.0的特性
查看>>
MySQL8修改密码报错ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
查看>>
MySQL8修改密码的方法
查看>>
Mysql8在Centos上安装后忘记root密码如何重新设置
查看>>
Mysql8在Windows上离线安装时忘记root密码
查看>>
MySQL8找不到my.ini配置文件以及报sql_mode=only_full_group_by解决方案
查看>>
mysql8的安装与卸载
查看>>
MySQL8,体验不一样的安装方式!
查看>>