|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
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 |
|---|
NewOngoingStubbing<T> thenReturn(T value)
when(mock.someMethod()).thenReturn(10);See examples in javadoc for
Mockito.when(T)
value - return value
NewOngoingStubbing<T> thenReturn(T value,
T... values)
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)
value - first return valuevalues - next return values
NewOngoingStubbing<T> thenThrow(java.lang.Throwable... throwables)
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)
throwables - to be thrown on method invocation
NewOngoingStubbing<T> thenAnswer(Answer<?> answer)
when(mock.someMethod(10)).thenAnswer(new Answer<Integer>() {
public Integer answer(InvocationOnMock invocation) throws Throwable {
return (Integer) invocation.getArguments()[0];
}
}
answer - the custom answer to execute.
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||