powermock series 2 - 測試私有方法,私有構(gòu)造函數(shù)

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. 設(shè)置內(nèi)部私有成員
  2. Use Whitebox.getInternalState(..) to get a non-public member of an instance or class. 獲取內(nèi)部私有成員
  3. Use Whitebox.invokeMethod(..) to invoke a non-public method of an instance or class. 調(diào)用私有方法
  4. 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);
    1. 構(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);
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 11,110評論 0 23
  • PLEASE READ THE FOLLOWING APPLE DEVELOPER PROGRAM LICENSE...
    念念不忘的閱讀 13,660評論 5 6
  • 越過山丘 提筆寫這篇文章的時(shí)候,我人生中第二次的高考剛剛結(jié)束?;蛟S你已經(jīng)猜到,我是一名復(fù)讀生。 ...
    不落123閱讀 372評論 0 1
  • 早上忙了一陣,吃過早飯已經(jīng)8點(diǎn)多,又檢查了一遍雨傘、水杯等,匆匆的趕車。嵊州至新昌的公交很多,路邊站一小會(huì)兒就來了...
    樂行樂知閱讀 751評論 0 1
  • “你不是我的親哥哥,你知道嗎?”梁梔唯哭著說。 “唯唯你別鬧了”梁沅澤就笑了 “我也很難過你知道嗎,可是,那樣我就...
    浪徒與酒閱讀 318評論 0 0

友情鏈接更多精彩內(nèi)容