为什么要使用PowerMock
现如今比较流行的Mock工具如jMock 、EasyMock 、Mockito等都有一个共同的缺点:不能mock静态、final、私有方法等。而PowerMock能够完美的弥补以上三个Mock工具的不足。
PowerMock简介
PowerMock是一个扩展了其它如EasyMock等mock框架的、功能更加强大的框架。PowerMock使用一个自定义类加载器和字节码操作来模拟静态方法,构造函数,final类和方法,私有方法,去除静态初始化器等等。通过使用自定义的类加载器,简化采用的IDE或持续集成服务器不需要做任何改变。熟悉PowerMock支持的mock框架的开发人员会发现PowerMock很容易使用,因为对于静态方法和构造器来说,整个的期望API是一样的。PowerMock旨在用少量的方法和注解扩展现有的API来实现额外的功能。目前PowerMock支持EasyMock和Mockito。
PowerMock入门
PowerMock有两个重要的注解:
–@RunWith(PowerMockRunner.class)
–@PrepareForTest( { YourClassWithEgStaticMethod.class })
如果你的测试用例里没有使用注解@PrepareForTest,那么可以不用加注解@RunWith(PowerMockRunner.class),反之亦然。当你需要使用PowerMock强大功能(Mock静态、final、私有方法等)的时候,就需要加注解@PrepareForTest。

PowerMock基本用法
(1) 普通Mock: Mock参数传递的对象
测试目标代码:

  1. public boolean callArgumentInstance(File file) {
  2.  
  3. return file.exists();
  4.  
  5. }

测试用例代码:

  1. @Test
  2. public void testCallArgumentInstance() {
  3.  
  4. File file = PowerMockito.mock(File.class);
  5.  
  6. ClassUnderTest underTest = new ClassUnderTest();
  7.  
  8. PowerMockito.when(file.exists()).thenReturn(true);
  9.  
  10. Assert.assertTrue(underTest.callArgumentInstance(file));
  11. }

说明:普通Mock不需要加@RunWith和@PrepareForTest注解。

(2)  Mock方法内部new出来的对象

测试目标代码:

  1. public class ClassUnderTest {
  2. public boolean callInternalInstance(String path) {
  3. File file = new File(path);
  4. return file.exists();
  5. }
  6. }

测试用例代码:

  1. @RunWith(PowerMockRunner.class)
  2. public class TestClassUnderTest {
  3. @Test
  4. @PrepareForTest(ClassUnderTest.class)
  5. public void testCallInternalInstance() throws Exception {
  6. File file = PowerMockito.mock(File.class);
  7. ClassUnderTest underTest = new ClassUnderTest();
  8. PowerMockito.whenNew(File.class).withArguments("bbb").thenReturn(file);
  9. PowerMockito.when(file.exists()).thenReturn(true);
  10. Assert.assertTrue(underTest.callInternalInstance("bbb"));
  11. }
  12. }

说明:当使用PowerMockito.whenNew方法时,必须加注解@PrepareForTest和@RunWith。注解@PrepareForTest里写的类是需要mock的new对象代码所在的类。

(3) Mock普通对象的final方法
  测试目标代码:

  1. public class ClassUnderTest {
  2. public boolean callFinalMethod(ClassDependency refer) {
  3. return refer.isAlive();
  4. }
  5. }
  6. public class ClassDependency {
  7. public final boolean isAlive() {
  8. // do something
  9. return false;
  10. }
  11. }

测试用例代码:

  1. @RunWith(PowerMockRunner.class)
  2. public class TestClassUnderTest {
  3. @Test
  4. @PrepareForTest(ClassDependency.class)
  5. public void testCallFinalMethod() {
  6. ClassDependency depencency = PowerMockito.mock(ClassDependency.class);
  7. ClassUnderTest underTest = new ClassUnderTest();
  8. PowerMockito.when(depencency.isAlive()).thenReturn(true);
  9. Assert.assertTrue(underTest.callFinalMethod(depencency));
  10. }
  11. }

说明: 当需要mock final方法的时候,必须加注解@PrepareForTest和@RunWith。注解@PrepareForTest里写的类是final方法所在的类。
(4) Mock普通类的静态方法
测试目标代码:

  1. public class ClassUnderTest {
  2. public boolean callStaticMethod() {
  3. return ClassDependency.isExist();
  4. }
  5. }
  6. public class ClassDependency {
  7. public static boolean isExist() {
  8. // do something
  9. return false;
  10. }
  11. }

测试用例代码:

  1. @RunWith(PowerMockRunner.class)
  2. public class TestClassUnderTest {
  3. @Test
  4. @PrepareForTest(ClassDependency.class)
  5. public void testCallStaticMethod() {
  6. ClassUnderTest underTest = new ClassUnderTest();
  7. PowerMockito.mockStatic(ClassDependency.class);
  8. PowerMockito.when(ClassDependency.isExist()).thenReturn(true);
  9. Assert.assertTrue(underTest.callStaticMethod());
  10. }
  11. }

说明:当需要mock静态方法的时候,必须加注解@PrepareForTest和@RunWith。注解@PrepareForTest里写的类是静态方法所在的类。

(5) Mock 私有方法
  测试目标代码:

  1. public class ClassUnderTest {
  2. public boolean callPrivateMethod() {
  3. return isExist();
  4. }
  5. private boolean isExist() {
  6. return false;
  7. }
  8. }

测试用例代码:

  1. @RunWith(PowerMockRunner.class)
  2. public class TestClassUnderTest {
  3.  
  4. @Test
  5. @PrepareForTest(ClassUnderTest.class)
  6. public void testCallPrivateMethod() throws Exception {
  7.  
  8. ClassUnderTest underTest = PowerMockito.mock(ClassUnderTest.class);
  9.  
  10. PowerMockito.when(underTest.callPrivateMethod()).thenCallRealMethod();
  11.  
  12. PowerMockito.when(underTest, "isExist").thenReturn(true);
  13.  
  14. Assert.assertTrue(underTest.callPrivateMethod());
  15.  
  16. }
  17. }

说明:和Mock普通方法一样,只是需要加注解@PrepareForTest(ClassUnderTest.class),注解里写的类是私有方法所在的类。

项目中的使用示例:

  1. private String validLstContainsCase(List<PlatformCase> caseLst, PlatformCase platformParam) {
  2. return msg;
  3. }
  4.  
  5. private CustomerProductPlatform createCustomerProductPlatform(Long platformCaseId) {
  6.  
  7. return param;
  8. }
  9.  
  10. @Test
  11. @PrepareForTest(PlatformCaseManageServiceImpl.class)
  12. public void validLstContainsCase(){
  13. try {
  14. PlatformCaseManageServiceImpl serviceImpl = PowerMockito.mock(PlatformCaseManageServiceImpl.class);
  15. // 模拟对象
  16. List<PlatformCase> caseLst = new ArrayList<>();
  17. PlatformCase plat = new PlatformCase();
  18. caseLst.add(plat);
  19.  
  20. PowerMockito.when(serviceImpl, "validLstContainsCase",caseLst,plat).thenReturn("test");
  21. } catch (Exception e) {
  22. e.printStackTrace();
  23. }
  24. }
  25.  
  26. @Test
  27. @PrepareForTest(PlatformCaseManageServiceImpl.class)
  28. public void createCustomerProductPlatform(){
  29. try {
  30. PlatformCaseManageServiceImpl serviceImpl = PowerMockito.mock(PlatformCaseManageServiceImpl.class);
  31.  
  32. PowerMockito.when(serviceImpl, "createCustomerProductPlatform",12l).thenReturn("test");
  33. } catch (Exception e) {
  34. e.printStackTrace();
  35. }
  36. }

(6) Mock系统类的静态和final方法
测试目标代码:

  1. public class ClassUnderTest {
  2. public boolean callSystemFinalMethod(String str) {
  3. return str.isEmpty();
  4. }
  5. public String callSystemStaticMethod(String str) {
  6. return System.getProperty(str);
  7. }
  8. }

测试用例代码:

  1. @RunWith(PowerMockRunner.class)
  2. public class TestClassUnderTest {
  3.  
  4. @Test
  5. @PrepareForTest(ClassUnderTest.class)
  6. public void testCallSystemStaticMethod() {
  7. ClassUnderTest underTest = new ClassUnderTest();
  8. PowerMockito.mockStatic(System.class);
  9. PowerMockito.when(System.getProperty("aaa")).thenReturn("bbb");
  10. Assert.assertEquals("bbb", underTest.callJDKStaticMethod("aaa"));
  11. }
  12. }

说明:和Mock普通对象的静态方法、final方法一样,只不过注解@PrepareForTest里写的类不一样 ,注解里写的类是需要调用系统方法所在的类。

无所不能的PowerMock
(1) 验证静态方法:
PowerMockito.verifyStatic();
Static.firstStaticMethod(param);
(2) 扩展验证:
PowerMockito.verifyStatic(Mockito.times(2)); // 被调用2次 Static.thirdStaticMethod(Mockito.anyInt()); // 以任何整数值被调用
(3) 更多的Mock方法
http://code.google.com/p/powermock/wiki/MockitoUsage13

PowerMock简单实现原理
• 当某个测试方法被注解@PrepareForTest标注以后,在运行测试用例时,会创建一个新org.powermock.core.classloader.MockClassLoader实例,然后加载该测试用例使用到的类(系统类除外)。
• PowerMock会根据你的mock要求,去修改写在注解@PrepareForTest里的class文件(当前测试类会自动加入注解中),以满足特殊的mock需求。例如:去除final方法的final标识,在静态方法的最前面加入自己的虚拟实现等。
• 如果需要mock的是系统类的final方法和静态方法,PowerMock不会直接修改系统类的class文件,而是修改调用系统类的class文件,以满足mock需求

PowerMock框架讲解及使用的更多相关文章

  1. 谷歌Volley网络框架讲解——BasicNetwork类

    谷歌Volley网络框架讲解——BasicNetwork类 这个类是toolbox工具箱包里的,实现了Network接口. 先来看下Network这个interface,performRequest( ...

  2. 程序员的自我救赎---1.4.2: 核心框架讲解(BLL&Tool)

    <前言> <目录> (一) Winner2.0 框架基础分析 (二) 短信中心 (三)SSO单点登录 (四)PLSQL报表系统 (五)钱包系统 (六)GPU支付中心 (七)权限 ...

  3. 框架原理第一讲,熟悉常用的设计方式.(以MFC框架讲解)

    框架原理第一讲,熟悉常用的设计方式.(以MFC框架讲解) 一丶什么是框架,以及框架的作用 什么是框架? 框架,简而言之就是把东西封装好了,使用框架开发可以快速开发程序,例如MFC程序的双击写代码. 为 ...

  4. 程序员的自我救赎---1.4.3: 核心框架讲解(MVC)

    <前言> (一) Winner2.0 框架基础分析 (二)PLSQL报表系统 (三)SSO单点登录 (四) 短信中心与消息中心 (五)钱包系统 (六)GPU支付中心 (七)权限系统 (八) ...

  5. 框架原理第三讲,RTTCreate,运行时类型创建.(以MFC框架讲解)

    框架原理第三讲,RTTCreate,运行时类型创建.(以MFC框架讲解) 通过昨天的讲解,我们已经理解了运行时类型识别是什么. 比如  CObject * pthis = (Cobject *)Cre ...

  6. 框架原理第二讲,RTTI,运行时类型识别.(以MFC框架讲解)

    框架原理第二讲,RTTI,运行时类型识别.(以MFC框架讲解) 一丶什么是RTTI,以及RTTI怎么设计 通过第一讲,我们知道了怎么样升成一个窗口了,以及简单的消息循环. 第二讲则是主要讲解RTTI ...

  7. 程序员的自我救赎---1.4.1:核心框架讲解(DAL)

    <前言> (一) Winner2.0 框架基础分析 (二)PLSQL报表系统 (三)SSO单点登录 (四) 短信中心与消息中心 (五)钱包系统 (六)GPU支付中心 (七)权限系统 (八) ...

  8. 通过Dapr实现一个简单的基于.net的微服务电商系统(二)——通讯框架讲解

    首先感谢张队@geffzhang公众号转发了上一篇文章,希望广大.neter多多推广dapr,让云原生更快更好的在.net这片土地上落地生根. 目录:一.通过Dapr实现一个简单的基于.net的微服务 ...

  9. 谷歌Volley网络框架讲解——第一篇

    自从公司新招了几个android工程师后,我清闲了些许.于是就可以有时间写写博客,研究一些没来的研究的东西. 今年的谷歌IO大会上,谷歌推出了自己的网络框架——Volley.不久前就听说了但是没有cl ...

随机推荐

  1. 【C++编程基础】(1)—— 函数原型声明、函数模板、引用、const 常引用、const 常量指针

    一.函数原型声明: 1.函数声明告诉编译器函数的名称,和如何调用函数(返回类型和参数):函数定义提供了函数的实际主体. 2.强制性的:在C++中,如果函数调用的位置在函数定义之前,则要求在函数调用之前 ...

  2. linux解压缩的常用命令

    1.解包:tar xvf filename.tar, 打包: tar cvf filename DirName 2.解压:gunzip filename.gz, tar zxvf filename.t ...

  3. 使用javac命令编译Servlet,并将其放入tomcat中运行

    首先我在桌面上新建了一个txt文件,编辑内容(内容来自菜鸟教程)为: // 导入必需的 java 库 import java.io.*; import javax.servlet.*; import ...

  4. jQuery循环之each()

    /** *定义和用法:$(selector).each(function(index,element)) *each()函数会对每个匹配到的元素运行函数(返回false可终止循环). *each()函 ...

  5. linux磁盘空间满了 但是没有大文件

    很常见的一个问题 linux磁盘空间满了 但是没有大文件 解决思路: 1.用df 检查发现/根目录可用空间为0 [root@/]#df -h 2.用du检查发现各目录占用的空间都很少,有约3G的空间莫 ...

  6. linux中网络部分的总结

    二.简述iproute家族命令 静态配置地址的方法有一下几种方式: (1)ifconfig (2)ip命令 (3)GUI工具 (4)TUI工具 (5)编辑配置文件 1.ifconfig 查看接口:if ...

  7. php中的Exception

    如果定制的EXCEPTION搞定了,默认的,就自然不在话下罗. 直接上最曲折的过程. InvalidIdException.php <?php namespace Bookstore\Excep ...

  8. P3398 仓鼠找sugar[LCA]

    题目描述 小仓鼠的和他的基(mei)友(zi)sugar住在地下洞穴中,每个节点的编号为1~n.地下洞穴是一个树形结构.这一天小仓鼠打算从从他的卧室(a)到餐厅(b),而他的基友同时要从他的卧室(c) ...

  9. jenkins邮件配置以及邮件添加附件详解

    1.在系统管理-系统设置  中找到邮件配置模块 填写情况如下图 第一步,填写系统管理员邮箱 第二步,填写邮箱配置 第三步,然后在项目中添加邮箱配置 项目中邮件设置中关于附件添加 因为我的项目目录中分3 ...

  10. nuxt 项目设置缩进为4个空格

    1..editorconfig 文件下的indent_size: 2更改为indent_size: 4 2..prettierrc 文件 { "singleQuote": true ...