Is it possible to call a method twice, call the real implementation first and then mock the results?
I am trying to verify that the recursive method is reselling correctly. Therefore, the first call should callRealMethod
. But the second call is just to check that it was called, and it shouldn't make a call, but should return a sealed result.
Is there a way to do this in Mockito?
+3
source to share
1 answer
You can simply use thenCallRealMethod followed by the usual thenReturn stub:
import org.junit.Test;
import static org.mockito.Mockito.*;
public class PartialMock {
String doIt() {
return "original";
}
@Test
public void testDoIt() {
PartialMock t = mock(PartialMock.class);
when(t.doIt())
.thenCallRealMethod()
.thenReturn("mocked");
assertEquals("original", t.doIt());
assertEquals("mocked", t.doIt());
assertEquals("mocked", t.doIt());
verify(t, times(3)).doIt();
}
}
+3
source to share