使用powermock单元测试静态方法 [英] unit testing static methods using powermock

查看:453
本文介绍了使用powermock单元测试静态方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为项目中的某些静态方法编写单元测试用例,

I want to write unit test case for some of the static methods in my project,

我的类代码的片段,

Class Util{
  public static String getVariableValue(String name)
  {
     if(isWindows()){
       return some string...
     }
     else{
       return some other string...
     }
  }

  public static boolean isWindows(){
    if(os is windows)
       return true;
    else
      return false; 
  }

}

基本上,我想写单位isWindows()返回 false时,getVariableValue()的测试用例。

Basically, i want to write unit test case for getVariableValue() when isWindows() returns 'false'. How do i write this using powermock?

推荐答案

// This is the way to tell PowerMock to mock all static methods of a
// given class
PowerMock.mockStaticPartial(Util.class,"isWindows");

expect(Util.isWindows()).andReturn(false);    

这篇关于使用powermock单元测试静态方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆