powerMock是基于easyMock或Mockito扩展出来的增强版本,所以powerMock分两种类型,如果你习惯于使用easyMock的,那你就下载基于easyMock的powerMock,反之你喜欢用mockito的话就下载另一种PowerMock.

powerMock之所以说它更强大是因为它解决了easyMock和Mockito没有解决的问题,就是可以模仿static,private和final的方法。举例如下:

public class User{

private User user=new User();

public User(){

System.out.println("new User()");

}

public static User getInstance(){

return user;

}

private String getHello(String h)

{

return "hello "+h;

}

public String sayHello(String h)

{

return getHello(h);

}

}

public classSubUser extends User{

public SubUser(){

System.out.println("new SubUser()");

}

}

测试用例,注意以下例子需要用junit4才能运行,如是不行的话建议重下一个jar包:junit-4.0.jar

@RunWith(PowerMockRunner.class)//powerMock包里有两个PowerMockRunner,所以如果你的不能运行,请更换另一个试试 @PrepareForTest(User.class)  public class TestUser extends TestCase  {  User user;    //测试静态方法   @Test  public void testVerify()  {      mockStatic(User.class);     when(User.getInstance()).thenReturn(new SubUser());    User user=User.getInstance();

verifyStatic();

}

//测试new一个对象

@Test      public void testWhenNew() {     try {     mockStatic(User.class);    whenNew(User.class).withNoArguments().thenReturn(new SubUser()) ;     user=new User();    verifyNew(User.class).withNoArguments();   } catch (Exception e) {    // TODO Auto-generated catch block    e.printStackTrace();   }

@Test//测试私有方法  public void testPrivate() {      User u = PowerMockito.spy(new User());             try {    PowerMockito.doReturn("hello world").when(u, "getHello", "hello");       System.out.println(u.sayHello("hello"));

PowerMockito.verifyPrivate(u, times(1)).invoke("getHello",  "hello");           } catch (Exception e) {    // TODO Auto-generated catch block    e.printStackTrace();   }          }

}

输出---------------------------------

new SubUser()

new SubUser()

hello world

其它用法请参考这里:http://code.google.com/p/powermock/wiki/MockitoUsage13

以上为个人的一点经验,有可能表述有误,敬请指正

http://blog.csdn.net/Ant_cc/article/details/6977367

http://dlwt.csdn.net/fd.php?i=686345406083133&s=8d1bea4adab5d744f78c6f60e13d3624

powerMock比easyMock和Mockito更强大(转)的更多相关文章

  1. PowerMock与EasyMock的应用(转)

    Leader请求在做Junit测试的时辰,Mock掉各个办法之间的依附.这两天进修了下PowerMock的应用. PowerMock是EasyMock的一个扩大,参加了static,final,pri ...

  2. Python的regex模块——更强大的正则表达式引擎

    Python自带了正则表达式引擎(内置的re模块),但是不支持一些高级特性,比如下面这几个: 固化分组    Atomic grouping 占有优先量词    Possessive quantifi ...

  3. 思维导图软件TheBrain 8全新发布 提供更强大的信息管理

    TheBrain思维导图软件是全球唯一一款动态的网状结构的思维导图软件,广泛用于学习.演讲.项目管理.会议.需求调研与分析等.其独特的信息组织方式使得用户可以创建并连接到数以万计的数字想法,为此在全球 ...

  4. cVim—Chrome上更强大的vim插件

    cVim——Chrome上更强大的vim插件 介绍 也许很多人在chrome上都用过类似Vimium, ViChrome的插件,这些插件的目的都差不多,就是在浏览器中提供一些类似vim的操作来提高效率 ...

  5. 功能更强大的格式化工具类 FormatUtils.java

    package com.util; import java.text.DecimalFormat; import java.text.ParseException; import java.text. ...

  6. 10个工具让你的 shell 脚本更强大

    10个工具让你的 shell 脚本更强大 很多人误以为shell脚本只能在命令行下使用.其实shell也可以调用一些GUI组件,例如菜单,警告框,进度条等等.你可以控制最终的输出,光标位 置还有各种输 ...

  7. 让Docker功能更强大的10个开源工具

    让Docker功能更强大的10个开源工具 更好的管理.Web前端程序.更深入地了解容器应用程序,Docker生态系统正在迅速发展,这还得归功于其充满活力的开源社区. 软件项目的成功常常根据其催生的生态 ...

  8. summerDao-比mybatis更强大无需映射配置的dao工具

    summerDao是summer框架中的一个数据库操作工具,项目地址:http://git.oschina.net/xiwa/summer. 怎么比mybatis更强大,怎么比beetlsql更简单, ...

  9. 比ngx_http_substitutions_filter_module 更强大的替换模块sregex的replace-filter-nginx-module

    之前写过nginx反代替换的教程(传送门),使用了ngx_http_substitutions_filter_module模块.不过这货只能替换同一行,具有局限性-_-# 现在一个更强大的替换模块来了 ...

随机推荐

  1. No.3小白的HTML+CSS心得篇

    A--看的东西多了总会出现好多模糊不清的又长的很像的的词语 今天对此进行区别分析下 1. align 与 text-align的区别 align 在W3Cschool中是这样解释的  ----alig ...

  2. 记载abp中Dbcontext的疑问

    q:abp中httpcontext如何在一次请求中保证获取的是相同的实例. 大牛的原话: LifestylePerWebRequest does not works good with async. ...

  3. Android学习之DragEvent

    关于DragEvent Google Android API中是这么说的 Represents an event that is sent out by the system at various t ...

  4. Centos6.4安装JDK

    链接地址:http://www.iteye.com/topic/1130311 1.先看看OpenJDK的安装包 $ rpm -qa |grep javatzdata-java-2013b-1.el6 ...

  5. 依赖于设备的位图(DDB) ,CreateCompatibleBitmap用法

    DDB(Device-dependent bitmap)依赖于具体设备,这主要体现在以下两个方面: DDB的颜色模式必需与输出设备相一致.例如,如果当前的显示设备是256色模式,那么DDB必然也是25 ...

  6. (3)选择元素——(2)文档对象模型(The Document Object Model)

    One of the most powerful aspects of jQuery is its ability to make selecting elements in the DOM easy ...

  7. MD5加密解密

    方法一 首先,先简单介绍一下MD5 MD5的全称是message-digest algorithm 5(信息-摘要算法,在90年代初由mit laboratory for computer scien ...

  8. MySQL 讨厌哪种类型的查询

    ㈠ 任何查询都讨厌             只要是查询.MySQL都讨厌.执行查询最快的方式就是不去执行        缓存为王.比如Redis或者memcache               ㈡ 查 ...

  9. 【Hibernate】set排序

    使用hibernate进行一对多操作的时候,普遍使用HashSet进行操作.但是HashSet是无序集合,对此可以使用TreeSet进行排序. 1.将HashSet改为TreeSet private ...

  10. python求微分方程组的数值解曲线01

    本人最近在写一篇关于神经网络同步的文章,其一部分模型为: x_i^{\Delta}(t)= -a_i*x_i(t)+ b_i* f(x_i(t))+ \sum\limits_{j \in\{i-1, ...