Powermockito 针对方法中new 对象的模拟,以及属性中new 对象的模拟
2024-08-30 00:37:08
PowerMocker 是一个功能逆天的mock 工具。
一,Powermockito 针对方法中new 对象的模拟
// 如何才能mock掉 WeChatConfigUtil 这个类,让 weChatConfigUtil.getMainApploginSwitch();这个方法返回你想要的结果
public void testA(){
WeChatConfigUtil weChatConfigUtil = new WeChatConfigUtil();
weChatConfigUtil.getMainAppLoginSwitch();
}
针对上述情况
package com.ppdai.wechat.sub.business.msgsub; import com.ppdai.wechat.sub.util.WeChatConfigUtil;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner; /**
* Created by huxuhong on 2020/7/2.
*/
//重点,要帮要mock的类(此例为weChatConfigUtil 所在的类TestC,要放到PrepareForTest中即可)
@PrepareForTest(
{ TestB.TestC.class
})
@RunWith(PowerMockRunner.class)
public class TestB {
public TestB(){ }
class TestC{
public void testA(){
WeChatConfigUtil weChatConfigUtil = new WeChatConfigUtil();
Boolean flag = weChatConfigUtil.getMainAppLoginSwitch();
System.out.println("mock结果"+flag);
}
} @Test
public void testTestC(){
TestC testC = new TestC();
WeChatConfigUtil weChatConfigUtil = PowerMockito.mock(WeChatConfigUtil.class);
try {
PowerMockito.whenNew(WeChatConfigUtil.class).withAnyArguments().thenReturn(weChatConfigUtil);
PowerMockito.doReturn(Boolean.TRUE).when(weChatConfigUtil).getMainAppLoginSwitch();
testC.testA();
} catch (Exception e) {
e.printStackTrace();
}
}
}
运行结果
二,如何解决属性中new 对象的模拟
public class TestB {
public TestB(){ }
class TestC{
//如何mock掉属性中new 对象(此例为WechatConfigUtil weChatConfigUtil = new WechatConfigUtil,让 weChatConfigUtil.getUserInfoApiUrl 能获取到指定的值)
WeChatConfigUtil weChatConfigUtil = new WeChatConfigUtil();
public void testA(){
String t = weChatConfigUtil.getUserInfoApiUrl();
System.out.println("mock结果"+t);
}
}
针对上述情况
public class TestB {
public TestB(){ }
class TestC{
//如果熟悉修饰为pubilc final static WechatConfigUtil,那么就需要和上面的例子一样要配置@PrepareForTest
WeChatConfigUtil weChatConfigUtil = new WeChatConfigUtil();
public void testA(){
String t = weChatConfigUtil.getUserInfoApiUrl();
System.out.println("mock结果"+t);
}
} @Test
public void testTestC(){
TestC testC = new TestC();
WeChatConfigUtil weChatConfigUtil = PowerMockito.mock(WeChatConfigUtil.class);
try {
Field weChatConfigUtilField = testC.getClass().getDeclaredField("weChatConfigUtil");
weChatConfigUtilField.setAccessible(Boolean.TRUE);
weChatConfigUtilField.set(testC,weChatConfigUtil);
PowerMockito.doReturn("testpc").when(weChatConfigUtil).getUserInfoApiUrl();
testC.testA();
} catch (Exception e) {
e.printStackTrace();
}
} }
运行结果
三,引入Powermock的时候注意和mockito的版本匹配问题
Powermock 使用过程中遇到的坑
①,使用Powermock mock 静态方法时,提示下面错误
org.mockito.exceptions.misusing.NotAMockException: Argument should be a mock, but is: class java.lang.Class
处理方式:版本问题,修改为 mockito 2.25.0,powermock 2.0.2,
issue https://github.com/powermock/powermock/issues/992#ref-commit-3479902
mockito 2.25.0和powermock 2.0.2
最新文章
- C#中的Lambda表达式的演化过程
- lua二进制操作函数
- swift 如何获取webView的内容高度
- Atlas+Keepalived系列二:管理Atlas
- C语言错误: HEAP CORRUPTION DETECTED
- TCP/IP WebSocket MQTT
- Microsoft Dynamics AX 2012 X++ Editor Extensions
- ANT的安装和配置(windows)
- CSS备忘-1
- 测试覆盖率工具EclEmma安装与使用
- SpringMVC工作原理 : HandlerMapping和HandlerAdapter
- Atiit 常见功能 常用功能与模块的最快速解决方案
- NIO和IO(BIO)的区别及NIO编程介绍
- Faster R-CNN在GPU下的安装、测试经历
- 【FastDFS】FastDFS在CentOS的搭建
- Vim打开文件中文乱码如何解决?
- jetty9优化的两处地方
- Java基础教程(19)--Object类
- 常用CSS缩写语法总结(转)
- 测试用Word2007发布博客文章
热门文章
- Java学习的第八天
- python日期与日历Datetime和Calendar模块
- [Luogu P3119] [USACO15JAN]草鉴定Grass Cownoisseur (缩点+图上DP)
- 【Luogu】 P5482 [JLOI2011]不等式组 题解
- 2018-12-8 论文翻译+hdoj+git+python
- JQuery cdn地址
- C/C++模运算(正负整数)
- ajax 加载数据前的刷新动画
- CSS3:overflow属性详解
- (数据科学学习手札98)纯Python绘制满满艺术感的山脊地图