无论如何,我们可以让一个模拟方法的行为有所不同,具体取决于该模拟方法被调用的次数?
例如。
如果你有一个方法叫做
public boolean mockedmMethod() {
    //logic here
}
你希望它以这种方式模拟:
当第一次调用 mockedMethod() 时,返回true。
第二次,第三次被调用...,返回false。
这能满足你的需要吗?
given(mock.mockedMethod())
        .willReturn(true, true, false, false)
        .willReturn(true)
        .willThrow(IllegalStateException.class)
        .will(execute_my_custom_answer());
    实际上,我发现:
Mockito.when(mockedMethod()).thenReturn(true).thenReturn(false);
也可以解决问题。