![](/img/trans.png)
[英]Verifying argument sent to method not invoked directly in mocked class using Mockito
[英]Using mocked method argument to mock next steps
我的代碼中有一個看起來像這樣的方法:
action.onResult(new Handler<MyClass>() {
@Override
public MyClass handle() { // Do something here }
}
}
我希望能夠模擬它(使用Mockito)。 像這樣:
when(mockedAction.onResult(any(Handler.class))).thenReturn(firstArg.handle());
意思是,我想調用發送給方法onResult
的參數的handle
方法。 我不能嘲笑處理程序,因為它使用了調用類的內部方法(我考慮過使用私有類,但尚未達到足夠好的解決方案)
動機 :這是一種異步回調機制,用於同步區域。 我想模擬對處理程序本身的調用,以便在代碼中同步繼續執行流程。
好的,未測試,但是在這種情況下可以使用ArgumentCaptor
:
final ArgumentCaptor<Handler> captor = ArgumentCaptor.forClass(Handler.class);
when(mock.onResult(captor.capture())).thenReturn(captor.getValue().handle());
但是,不確定捕獲者是否具有在此處初始化的“時間”。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.