我是靠谱客的博主 怕孤独过客,最近开发中收集的这篇文章主要介绍Mockito(mockito-inline ^3.4.0) mockStatic 模拟无参、有参静态方法调用,觉得挺不错的,现在分享给大家,希望可以做个参考。
概述
Mockito早期一直不支持静态方法mock测试,需要借助PowerMockito实现静态方法mock测试。
自mockito 3.4.0开始,支持静态方法mock测试,用法简单方便。下面示例演示有参和无参的静态方法调用mock测试和验证。
本文测试环境:
jdk11 + mockito4.0.0 + junit5 + spring-boot2.6.5
maven依赖:
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-inline</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
单测类示例:
package tom.demo.mockito;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentMatchers;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
import org.mockito.internal.invocation.InterceptedInvocation;
import org.mockito.internal.stubbing.answers.Returns;
import org.mockito.internal.verification.Times;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Objects;
/**
* 以前使用PowerMockito来mock静态方法,自Mockito 3.4.0 以后不再需要PowerMockito, Mockito本身已经支持
* <br>
* <code>https://javadoc.io/doc/org.mockito/mockito-core</code>
*/
public class MockStaticDemo {
@Test
void testStatic1() {
MockedStatic<StaticDemo>
m = Mockito.mockStatic(StaticDemo.class);
m.when(StaticDemo::hello).thenReturn("mock-hello").thenCallRealMethod();
Assertions.assertEquals(StaticDemo.hello(), "mock-hello");
Assertions.assertEquals(StaticDemo.hello(), "hello");
m.verify(StaticDemo::hello, new Times(2));
m.close();
}
@Test
void testStatic2() {
MockedStatic<StaticDemo>
m = Mockito.mockStatic(StaticDemo.class, new Returns("mock-returns"));
Assertions.assertEquals(StaticDemo.hello(), "mock-returns");
Assertions.assertEquals(StaticDemo.hi("tom"), "mock-returns");
m.close();
}
@Test
void testStatic3() {
MockedStatic<StaticDemo>
m = Mockito.mockStatic(StaticDemo.class, invocation -> {
Object[] args = invocation.getArguments(); // 调用方法的参数
Object mock = invocation.getMock(); // 被mock的静态类的class信息,不是静态类本身
Method method = invocation.getMethod(); // 被调用的具体方法
String location = ((InterceptedInvocation) invocation).getLocation().toString(); // 调用具体位置
System.out.println(location + " invoke method: " + method.getName()
+ " with arguments: " + Arrays.toString(args)
+ " return type: " + method.getReturnType().getName()
);
if (Objects.equals(method.getName(), "hello")) {
return "mock-hello";
} else if (Objects.equals(method.getName(), "hi")) {
return "mock-hi" + Arrays.toString(args);
}
return location + " " + method.getName() + " called with arguments: " + Arrays.toString(args);
});
Assertions.assertEquals(StaticDemo.hello(), "mock-hello");
System.out.println(StaticDemo.hi("cat"));
System.out.println(StaticDemo.hi("dog"));
System.out.println(StaticDemo.hi("dog"));
System.out.println(StaticDemo.eat("elephant"));
m.verify(StaticDemo::hello, new Times(1));
m.verify(() -> {
StaticDemo.hi("cat"); // 验证方法 hi() 调用参数cat 只调用了1次
}, new Times(1));
m.verify(() -> {
StaticDemo.hi("dog"); // 验证方法 hi() 调用参数dog 只调用了2次
}, new Times(2));
m.verify(() -> {
StaticDemo.hi(ArgumentMatchers.anyString()); // 验证方法 hi() 调用参数任意string串 调用了3次
}, new Times(3));
m.close();
}
static class StaticDemo {
static String hello() {
return "hello";
}
static String hi(String name) {
return "hi " + name;
}
static String eat(String name) {
return "eat " + name;
}
}
}
参考:
mockito-core3.5.10
最后
以上就是怕孤独过客为你收集整理的Mockito(mockito-inline ^3.4.0) mockStatic 模拟无参、有参静态方法调用的全部内容,希望文章能够帮你解决Mockito(mockito-inline ^3.4.0) mockStatic 模拟无参、有参静态方法调用所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复