org.mockito.internal.progress
Interface NewOngoingStubbing<T>


public interface NewOngoingStubbing<T>

Simply put: "When the x method is called then return y". E.g:

 when(mock.someMethod()).thenReturn(10);

 //you can use flexible argument matchers, e.g:
 when(mock.someMethod(anyString())).thenReturn(10);

 //setting exception to be thrown:
 when(mock.someMethod("some arg")).thenThrow(new RuntimeException());

 //you can set different behavior for consecutive method calls.
 //Last stubbing (e.g: thenReturn("foo")) determines the behavior of further consecutive calls.
 when(mock.someMethod("some arg"))
  .thenThrow(new RuntimeException())
  .thenReturn("foo");
 
 //There is a shorter way of consecutive stubbing:
 when(mock.someMethod()).thenReturn(1,2,3);
 when(mock.otherMethod()).thenThrow(exc1, exc2);
 
See examples in javadoc for Mockito.when(T)


Method Summary
 NewOngoingStubbing<T> thenAnswer(Answer<?> answer)
          Sets a generic Answer for the method.
 NewOngoingStubbing<T> thenReturn(T value)
          Sets a return value to be returned when the method is called.
 NewOngoingStubbing<T> thenReturn(T value, T... values)
          Sets consecutive return values to be returned when the method is called.
 NewOngoingStubbing<T> thenThrow(java.lang.Throwable... throwables)
          Sets Throwable objects to be thrown when the method is called.
 

Method Detail

thenReturn

NewOngoingStubbing<T> thenReturn(T value)
Sets a return value to be returned when the method is called. E.g:
 when(mock.someMethod()).thenReturn(10);
 
See examples in javadoc for Mockito.when(T)

Parameters:
value - return value
Returns:
ongoingStubbing object that allows stubbing consecutive calls

thenReturn

NewOngoingStubbing<T> thenReturn(T value,
                                 T... values)
Sets consecutive return values to be returned when the method is called. E.g:
 when(mock.someMethod()).thenReturn(1, 2, 3);
 
Last return value in the sequence (in example: 3) determines the behavior of further consecutive calls.

See examples in javadoc for Mockito.when(T)

Parameters:
value - first return value
values - next return values
Returns:
ongoingStubbing object that allows stubbing consecutive calls

thenThrow

NewOngoingStubbing<T> thenThrow(java.lang.Throwable... throwables)
Sets Throwable objects to be thrown when the method is called. E.g:
 when(mock.someMethod()).thenThrow(new RuntimeException());
 
If throwables contain a checked exception then it has to match one of the checked exceptions of method signature.

You can specify throwables to be thrown for consecutive calls. In that case the last throwable determines the behavior of further consecutive calls.

if throwable is null then exception will be thrown.

See examples in javadoc for Mockito.when(T)

Parameters:
throwables - to be thrown on method invocation
Returns:
ongoingStubbing object that allows stubbing consecutive calls

thenAnswer

NewOngoingStubbing<T> thenAnswer(Answer<?> answer)
Sets a generic Answer for the method. E.g:
 when(mock.someMethod(10)).thenAnswer(new Answer<Integer>() {
     public Integer answer(InvocationOnMock invocation) throws Throwable {
         return (Integer) invocation.getArguments()[0];
     }
 }
 

Parameters:
answer - the custom answer to execute.
Returns:
ongoingStubbing object that allows stubbing consecutive calls