大家好,我是神韵,是一个技术&生活博主。出文章目的主要是两个,一是好记忆不如烂笔头,记录总结中提高自己。二是希望我的文章可以帮到大家。欢迎来点赞打卡,你们的行动将是我无限的动力。
本篇主题是:
Mockito — 如何Mock Util类中的static 方法?
比如要Mock下面的DateUtils类中static-convertLocalDateTimeToStr方法,下面只是一个例子,真实场景中可能非常复杂的xxUtils
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class DateUtils {
public static String convertLocalDateTimeToStr(LocalDateTime localDateTime) {
// 比较复杂的业务 假设~
// 转化
DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
return localDateTime.format(dateFormatter);
}
}
1、引入依赖
注意要引入mockito-inline依赖代替mockito-core,不然会报错
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.1</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-inline</artifactId>
</dependency>
</dependencies>
2、编写测试类
@RunWith(MockitoJUnitRunner.class)
public class DateUtilsTest {
@Test
public void testConvertLocalDateTimeToStr() {
String expectResult = "mock static method successful";
MockedStatic<DateUtils> dateUtilsMockedStatic = Mockito.mockStatic(DateUtils.class);
dateUtilsMockedStatic.when(() -> DateUtils.convertLocalDateTimeToStr(Mockito.any())).thenReturn(expectResult);
String result = DateUtils.convertLocalDateTimeToStr(LocalDateTime.now());
Assert.assertEquals(expectResult, result);
}
}
用Mockito.mockStatic对DateUtils进行mock之后,用when模拟相应行为就可以。例子中mock之后使之响应”mock static method successful”内容,然后用Assert对比mock是否成功。
3、如果要多个测试类中使用,需要close掉mock static,不然会报只能在当前线程使用
dateUtilsMockedStatic.close();
@RunWith(MockitoJUnitRunner.class)
public class DateUtilsTest {
@Test
public void testConvertLocalDateTimeToStr() {
String expectResult = "mock static method successful";
MockedStatic<DateUtils> dateUtilsMockedStatic = Mockito.mockStatic(DateUtils.class);
dateUtilsMockedStatic.when(() -> DateUtils.convertLocalDateTimeToStr(Mockito.any())).thenReturn(expectResult);
String result = DateUtils.convertLocalDateTimeToStr(LocalDateTime.now());
Assert.assertEquals(expectResult, result);
dateUtilsMockedStatic.close();
}
@Test
public void testConvertLocalDateTimeToStr2() {
String expectResult = "mock static method successful";
MockedStatic<DateUtils> dateUtilsMockedStatic = Mockito.mockStatic(DateUtils.class);
dateUtilsMockedStatic.when(() -> DateUtils.convertLocalDateTimeToStr(Mockito.any())).thenReturn(expectResult);
String result = DateUtils.convertLocalDateTimeToStr(LocalDateTime.now());
Assert.assertEquals(expectResult, result);
dateUtilsMockedStatic.close();
}
}
注意
1、要引入mockito-inline依赖代替mockito-core,不然会报下面错误
org.mockito.exceptions.base.MockitoException:
The used MockMaker SubclassByteBuddyMockMaker does not support the creation of static mocksMockito’s inline mock maker supports static mocks based on the Instrumentation API.
You can simply enable this mock mode, by placing the ‘mockito-inline’ artifact where you are currently using ‘mockito-core’.
Note that Mockito’s inline mock maker is not supported on Android.
2、多个测试类没有close当前mockStatic时,会报下面错误
org.mockito.exceptions.base.MockitoException:
For pers.xue.skills.util.DateUtils, static mocking is already registered in the current threadTo create a new mock, the existing static mock registration must be deregistered