【问题标题】:Mocking a static final method using PowerMock使用 PowerMock 模拟静态最终方法
【发布时间】:2012-01-12 17:37:34
【问题描述】:

我有一个包含所有静态和最终方法的类。如何模拟类中的静态和最终方法。

public class UtilityClass{
  public static final void utilityMethod(){
  }

  public static final Integer anotherUtilMethod(){
  }
}

如何使用 PowerMock 模拟 utilMethod()anotherUtilMethod()

【问题讨论】:

    标签: unit-testing mocking static-methods final powermock


    【解决方案1】:
    final UtilityClass util = PowerMockito.mock(UtilityClass.class);
    PowerMockito.mockStatic(UtilityClass.class);
    

    然后像往常一样使用 PowerMockito.when()。查看http://code.google.com/p/powermock/wiki/MockitoUsage13 的示例。

    【讨论】:

    • 为什么一定要把第一行放在那里?
    • @michael 没有足够的信息让这个答案有帮助。请添加更多详细信息。
    • 在 Class 为 final 且方法为 static final 的情况下不起作用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-03
    • 2012-02-09
    • 1970-01-01
    • 1970-01-01
    • 2019-10-28
    相关资源
    最近更新 更多