我正在使用Mockito來模擬我正在編寫測試的同一個類中的方法。我在SO上看到了其他的答案(Mocking method in the same class),但可能我誤解了他們,因爲我遇到了問題。同類中的模擬方法
class Temp() {
public boolean methodA(String param) {
try {
if(methodB(param))
return true;
return false;
} catch (Exception e) {
e.printStackTrace();
}
}
}
我的測試方法:
@Test
public void testMethodA() {
Temp temp = new Temp();
Temp spyTemp = Mockito.spy(temp);
Mockito.doReturn(true).when(spyTemp).methodB(Mockito.any());
boolean status = temp.methodA("XYZ");
Assert.assertEquals(true, status);
}
我卻因的methodB的定義被執行得到厚望打印出來。 我的理解是methodB的定義會被spyTemp嘲笑。然而,看起來並非如此。
有人可以請解釋我要去哪裏嗎?