PowerMock简介

  Java单元测试框架主要有JUnit和TestNG,常用的Mock框架主要有EasyMock、Mockito和JMock等,那么为什么还要有PowerMock呢?实际上,PowerMock并不是发明了一套新的Mock框架,而是在原有的几种框架基础上进行扩充,补充完成原有框架所不能完成的工作,从PowerMock的ReadMe文档中可以看出这点:

PowerMock is a framework that extends other mock libraries such as EasyMock with more powerful capabilities. PowerMock uses a custom classloader and bytecode manipulation to enable mocking of static methods, constructors, final classes and methods, private methods, removal of static initializers and more. By using a custom classloader no changes need to be done to the IDE or continuous integration servers which simplifies adoption. Developers familiar with the supported mock frameworks will find PowerMock easy to use, since the entire expectation API is the same, both for static methods and constructors. PowerMock aims to extend the existing API's with a small number of methods and annotations to enable the extra features. Currently PowerMock supports EasyMock and Mockito.

  可以看到PowerMock是利用Java的类加载器和字节码操作来mock静态方法、构造函数、final类和方法、私有方法、移除静态初始化等。熟悉mock框架的开发者会发现Powermock易于使用,因为其所有的API都是相同的,包括静态方法和构造函数。Powermock的目的就是在原有API的基础上,增加少量的方法和注解来完成特别的功能。
  但是使用Powermock时要注意,其并不是对所有的框架都兼容,当前只支持EasyMock和Mockito,而且对所依赖的Jar包要求很严格,如果版本不匹配,经常会出现一些莫名冲突的问题, 当前最新的版本为1.7.3。

你可能感兴趣的:(PowerMock简介)