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
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.
- Use
Whitebox.setInternalState(..)to set a non-public member of an instance or class. 設(shè)置內(nèi)部私有成員 - Use
Whitebox.getInternalState(..)to get a non-public member of an instance or class. 獲取內(nèi)部私有成員 - Use
Whitebox.invokeMethod(..)to invoke a non-public method of an instance or class. 調(diào)用私有方法 - Use
Whitebox.invokeConstructor(..)to create an instance of a class with a private constructor. 調(diào)用私有構(gòu)造函數(shù)
舉例
- 1 調(diào)用私有方法
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;
}
...
根據(jù)傳入?yún)?shù)class判斷用哪個(gè)
int result = Whitebox.<Integer> invokeMethod(myInstance, new Class<?>[]{int.class}, "myMethod", 1);
- 3 調(diào)用私有構(gòu)造器
public class PrivateConstructorInstantiationDemo {
private final int state;
private PrivateConstructorInstantiationDemo(int state) {
this.state = state;
}
public int getState() {
return state;
}
}
調(diào)用:
PrivateConstructorInstantiationDemo instance = WhiteBox.invokeConstructor(
PrivateConstructorInstantiationDemo.class, 43);
- 構(gòu)造器重載的情況
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;
}
}
調(diào)用:
PrivateConstructorInstantiationDemo instance = Whitebox.invokeConstructor(PrivateConstructorInstantiationDemo.class, new Class<?>[]{Integer.class}, 43);