2 回答

TA貢獻1860條經驗 獲得超8個贊
我正在嘗試做一些非常類似的事情,以驗證從withHandle測試中的模擬 JDBI 調用傳遞給另一個模擬的參數。
您在問題中給出的答案為我指明了正確的方向,但給了我錯誤消息:
The method then(Answer<?>) in the type OngoingStubbing<Object> is not applicable for the arguments ((<no type> invocationOnMock) -> {})
相反,我不得不使用新的org.mockito.stubbing.Answer傳遞給then,類似于您鏈接到的另一個問題。
在您的示例中,這將類似于:
when(JDBIMock.withHandle(any())).then(
//Answer<Void> lambda
new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
Object[] args = invocation.getArguments();
assertEquals(1, args.length);
//the interface def for the callback passed to JDBI
HandleCallback lambda = (HandleCallback) args[0];
when(mockHandle.attach(SomeDao.class)).thenReturn(mockDao);
//this actually invokes my lambda, which implements the JDBI interface, with a mock argument
lambda.withHandle(mockHandle);
//bingo!
verify(mockDao).findSomethingInDB(eq(args));
return null; // to match the Void type
}
}
)
在我的情況下,我期待一個結果列表,withHandle所以我不得不更改Answer類型,并返回類型answer以匹配并返回一個虛擬列表而不是Void. (在這個測試中返回的實際結果并不重要,只是將預期的參數傳遞給我隨后的模擬對象)。
我還將verify調用移到了Answer測試的主體中,因此更清楚的是這是測試的預期,而不是模擬設置的一部分。
添加回答
舉報