美文网首页
powermock series 2 - 测试私有方法,私有构造

powermock series 2 - 测试私有方法,私有构造

作者: 尖头核桃 | 来源:发表于2017-12-06 17:59 被阅读0次

Getting Started

use : rely on EasyMock or Mockito and a test framework

Bypass Encapsulation mock 绕过封装

翻译自
https://github.com/powermock/powermock/wiki/Bypass-Encapsulation

Quick summary

Whitebox

class provides a set of methods which could you help bypass encapsulation if it required. Usually, it's not a good idea to get/modify non-public fields, but sometimes it's only way to cover code by test for future refactoring.

  1. Use Whitebox.setInternalState(..) to set a non-public member of an instance or class. 设置内部私有成员
  2. Use Whitebox.getInternalState(..) to get a non-public member of an instance or class. 获取内部私有成员
  3. Use Whitebox.invokeMethod(..) to invoke a non-public method of an instance or class. 调用私有方法
  4. Use Whitebox.invokeConstructor(..) to create an instance of a class with a private constructor. 调用私有构造函数

举例

  • 1 调用私有方法
private int sum(int a, int b) {
    return a+b;
}
int sum = Whitebox.<Integer> invokeMethod(myInstance, "sum", 1, 2);
  • 2 私有方法重载情况
...
private int myMethod(int id) {      
    return 2*id;
}

private int myMethod(Integer id) {      
        return 3*id;
}
...

根据传入参数class判断用哪个

int result = Whitebox.<Integer> invokeMethod(myInstance, new Class<?>[]{int.class}, "myMethod", 1);
  • 3 调用私有构造器
public class PrivateConstructorInstantiationDemo {

    private final int state;

    private PrivateConstructorInstantiationDemo(int state) {
        this.state = state;
    }

    public int getState() {
        return state;
    }
}

调用:

PrivateConstructorInstantiationDemo instance =  WhiteBox.invokeConstructor(
                PrivateConstructorInstantiationDemo.class, 43);
    1. 构造器重载的情况
public class PrivateConstructorInstantiationDemo {

    private final int state;

    private PrivateConstructorInstantiationDemo(int state) {
        this.state = state;
    }

    private PrivateConstructorInstantiationDemo(Integer state) {
              this.state = state;
              // do something else
    }

    public int getState() {
        return state;
    }
}

调用:

PrivateConstructorInstantiationDemo instance = Whitebox.invokeConstructor(PrivateConstructorInstantiationDemo.class, new Class<?>[]{Integer.class}, 43);

相关文章

  • powermock series 2 - 测试私有方法,私有构造

    Getting Started use : rely on EasyMock or Mockito and a t...

  • java构造方法私有化

    理解构造方法私有化过程 - 1. 构造方式私有化 当构造方法私有化后,外部类无法对其进行实例化 - 2. 在类内部...

  • 单例模式

    使用一个私有构造方法、一个私有静态变量以及一个共有静态方法实现。私有构造方法保证了不能通过构造方法来创建对象实例,...

  • JavaScript 构造函数私有、公有、静态、特权方法

    3. 构造函数的私有、公有、静态、特权方法 私有属性和方法创建:在构造函数内部创建的变量是私有属性,创建的函数是私...

  • 构造函数

    构造函数分为: 1.实例构造函数 2.私有构造函数 3.静态构造函数 私有构造函数 私有构造函数是一种特殊的实例构...

  • java 单例设计模式

    1.传统的两私有一公开(私有构造方法、私有静态实例(懒实例化/直接实例化)、公开的静态获取方法)涉及线程安全问题(...

  • Python 之路06 - 面向对象

    本节大纲: 特性:class、object 封装 继承 多态语法\属性\方法构造函数\析构函数私有方法\私有属性类...

  • python 13面向对象

    构造函数 析构函数 私有 类方法 静态方法 属性方法

  • 2017年3月7号,java单例模式

    /* * 饿汉模式 */ abstract class SigletonPattern { //私有构造方法,不能...

  • 强化Singleton属性

    法则:用私有构造器或枚举类型强化Singleton属性 实现Singleton的三种方法: 把构造器保持为私有的,...

网友评论

      本文标题:powermock series 2 - 测试私有方法,私有构造

      本文链接:https://www.haomeiwen.com/subject/jaslixtx.html