Mockito
method when()
public static <T> OngoingStubbing<T> when(T methodCall){
return MOCKITO_CORE.when(methodCall);
}
public <T> OngoingStubbing<T> when(T methodCall) {
MockingProgress mockingProgress = mockingProgress();
mockingProgress.stubbingStarted();
@SuppressWarnings("unchecked")
OngoingStubbing<T> stubbing = (OngoingStubbing<T>) mockingProgress.pullOngoingStubbing();
if (stubbing == null) {
mockingProgress.reset();
throw missingMethodInvocation();
}
return stubbing;
}
@Override
public OngoingStubbing<T> thenReturn(T value) {
return thenAnswer(new Returns(value));
}
@Override
public OngoingStubbing<T> thenReturn(T value, T... values) {
OngoingStubbing<T> stubbing = thenReturn(value);
if (values == null) {
// For no good reason we're configuring null answer here
// This has been like that since forever, so let's keep it for compatibility (unless
// users complain)
return stubbing.thenReturn(null);
}
for (T v : values) {
stubbing = stubbing.thenReturn(v);
}
return stubbing;
}
public class OngoingStubbingImpl<T> extends BaseStubbing<T> {
private final InvocationContainerImpl invocationContainer;
private Strictness strictness;
public OngoingStubbingImpl(InvocationContainerImpl invocationContainer) {
super(invocationContainer.invokedMock());
this.invocationContainer = invocationContainer;
@Override
public OngoingStubbing<T> thenAnswer(Answer<?> answer) {
if (!invocationContainer.hasInvocationForPotentialStubbing()) {
throw incorrectUseOfApi();
}
invocationContainer.addAnswer(answer, strictness);
return new ConsecutiveStubbing<T>(invocationContainer);
}
......
}