3 回答

TA貢獻(xiàn)1789條經(jīng)驗(yàn) 獲得超8個(gè)贊
Mockito 提供了一個(gè)方法doNothing()來(lái)模擬對(duì) void 方法的調(diào)用。對(duì)于上面的問(wèn)題 void 方法deleteFromRealm可以模擬為
Mockito.doNothing().when(mockedObject).deleteFromRealm();
因?yàn)閐eleteFromRealm是最終的,不能被嘲笑。模擬同一類的方法deleteObject
ClassToBeTested testClass = new ClassToBeTested();
ClassToBeTested testClassSpy = PowerMockito.spy(testClass);
PowerMockito.doNothing().when(testClassSpy, "deleteObject", mockedRealm);

TA貢獻(xiàn)1785條經(jīng)驗(yàn) 獲得超4個(gè)贊
我認(rèn)為第三個(gè)參數(shù)when
應(yīng)該是一個(gè)模擬匹配器:
PowerMockito.doNothing().when(store, "deleteObject", eq(mockedObject));

TA貢獻(xiàn)2080條經(jīng)驗(yàn) 獲得超4個(gè)贊
也許為時(shí)已晚,但您的問(wèn)題的可能解決方案可能如下所示:
PowerMockito.doNothing().when(store, "deleteObject", Matchers.any(RealmObject.class));
親切的問(wèn)候
添加回答
舉報(bào)