第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在不創(chuàng)建新對象的情況下測試私有成員對象

如何在不創(chuàng)建新對象的情況下測試私有成員對象

胡子哥哥 2023-06-04 14:55:53
我正在嘗試針對一個類編寫單元測試。我無法更改課程,但我認為可以使用反射進行測試。我只是不知道該怎么做。這是課程:public class MyClass extends AnotherClass implements TheInterface{    private enum SomeTypes    {        SAMPLE01, SAMPLE02, SAMPLE03    }    private CircularList<SomeTypes> someTypesList;     Date date= new Date();    private SomeOtherClassProcessor01 someOtherClassProcessor01;    private SomeOtherClassProcessor02 someOtherClassProcessor02;    private SomeOtherClassProcessor03 someOtherClassProcessor03;    public Properties initialize (Properties properties) throws Exception    {        Properties propertiesToReturn = super.initialize(properties);        someTypesList = new CircularList<SomeTypes>    (Arrays.asList(SomeTypes.values()));         someOtherClassProcessor01 = new SomeOtherClassProcessor01();         someOtherClassProcessor02 = new SomeOtherClassProcessor02();         someOtherClassProcessor03 = new SomeOtherClassProcessor03();         return propertiesToReturn;    }    @Override    public void get(ImportedClass someParams) throws Exception    {        SomeTypes types = someTypesList.getFirstAndRotate();        switch(types)        {            case SAMPLE01:            someOtherClassProcessor01.doSomething(someParams,     date);            break;            case SAMPLE02:            someOtherClassProcessor02.doSomething(someParams,     date);            break;            case SAMPLE03:            someOtherClassProcessor03.doSomething(someParams,     date);            break;            default:            throw new IllegalArgumentException("This " + types + "     was not implemented.");        }    }   }是否可以為此使用白盒?我需要確保在這些對象的 getter 中有一個調(diào)用。我應該嘗試 when(someOtherClassProcessor01.doSomething(any(), date)).thenReturn(true) 之類的東西嗎?如果您需要更多詳細信息,請告訴我。
查看完整描述

1 回答

?
尚方寶劍之說

TA貢獻1788條經(jīng)驗 獲得超4個贊

一種選擇是將您自己的(模擬的)實現(xiàn)替換SomeOtherClassProcessor為MyClass使用反射:


MyClass myClass = new MyClass();

SomeOtherProcessor01 mockProcessor01 = mock(SomeOtherProcessor01.class);


// reflection bit: find the field by its name

// handle NoSuchFieldException

Field someProcessorField = MyClass.getDeclaredField("someOtherProcessor01");

// the field is declared as private, so make it accessible in order to work with it

someProcessorField.setAccessible(true);

// now set your mocked processor into the field. 

// First argument is the object to change; second argument - new value for the field

someProcessorField.set(myClass, mockProcessor01);

附言。使用 PowerMock 和/或反射是向糟糕的設(shè)計投降(根據(jù) Timothy :)。您不應該依賴尚未經(jīng)過充分測試的代碼,如果是,您不應該再次嘗試對其進行測試。假設(shè)您的測試實際上揭示了一個錯誤——如果您不控制代碼,您將如何修復它?假設(shè) Java 11 變成了一個東西,禁止你使用反射。假設(shè)您正在測試的代碼發(fā)生變化并且字段被重命名 - 通過反射,您沒有編譯時安全......潛在問題列表繼續(xù)


查看完整回答
反對 回復 2023-06-04
  • 1 回答
  • 0 關(guān)注
  • 138 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號