我試圖測試這個方法,看看是否searchProfile被稱爲不帶參數:的java的Mockito - 使用不同的參數測試方法調用
public void searchProfile(Long searchTerm) {
this.searchTerm = searchTerm;
searchProfile();
}
public void searchProfile() {
//...
}
這是我的測試情況下,我所說的方法只有一個參數並期望沒有論據的人被稱爲
@Test
public void testSearchProfile() throws Exception {
CustomerProfileController sutStub = Mockito.mock(CustomerProfileController.class);
doNothing().when(sutStub).searchProfile();
sutStub.searchProfile(0L);
verify(sutStub, times(1)).searchProfile();
}
我該如何做這項工作?現在,它只是給我一個錯誤:
Comparison Failure:
Expected: customerProfileController.searchProfile();
Actual: customerProfileController.searchProfile(0);
我想接受的答案是正確的,它可以解決你的問題。但爲什麼你想測試一個模擬?測試一個具體的CustomerProfileController實例並嘲笑它的依賴性會不會更好?通常情況下,模擬測試的方式是什麼? – Magnilex
@Magnilex你是對的,我想用間諜方式代替。經過一番研究,我已經將測試實施全部更改了。謝謝! – Randy
是的,我正要添加一個答案,說間諜可能是一個更好的選擇,但問題沒有足夠的上下文讓我這樣做。 – Magnilex