org.mockito.stubbing
public interface Stubber
Example:
doThrow(new RuntimeException()).when(mockedList).clear();
//following throws RuntimeException:
mockedList.clear();
Also useful when stubbing consecutive calls:
doThrow(new RuntimeException("one")).
doThrow(new RuntimeException("two"))
.when(mock).someVoidMethod();
Read more about those methods:
See examples in javadoc for Mockito
Modifier and Type | Method and Description |
---|---|
Stubber |
doAnswer(Answer answer)
Use it for stubbing consecutive calls in
Mockito.doAnswer(Answer) style:
|
Stubber |
doCallRealMethod()
Use it for stubbing consecutive calls in
Mockito.doCallRealMethod() style. |
Stubber |
doNothing()
Use it for stubbing consecutive calls in
Mockito.doNothing() style:
|
Stubber |
doReturn(Object toBeReturned)
Use it for stubbing consecutive calls in
Mockito.doReturn(Object) style. |
Stubber |
doReturn(Object toBeReturned,
Object... nextToBeReturned)
Use it for stubbing consecutive calls in
Mockito.doReturn(Object) style. |
Stubber |
doThrow(Class<? extends Throwable> toBeThrown)
Use it for stubbing consecutive calls in
Mockito.doThrow(Class) style:
|
Stubber |
doThrow(Class<? extends Throwable> toBeThrown,
Class<? extends Throwable>... nextToBeThrown)
Use it for stubbing consecutive calls in
Mockito.doThrow(Class) style:
|
Stubber |
doThrow(Throwable... toBeThrown)
Use it for stubbing consecutive calls in
Mockito.doThrow(Throwable[]) style:
|
<T> T |
when(T mock)
Allows to choose a method when stubbing in doThrow()|doAnswer()|doNothing()|doReturn() style
|
<T> T when(T mock)
Example:
doThrow(new RuntimeException())
.when(mockedList).clear();
//following throws RuntimeException:
mockedList.clear();
Read more about those methods:
See examples in javadoc for Mockito
mock
- The mockStubber doThrow(Throwable... toBeThrown)
Mockito.doThrow(Throwable[])
style:
doThrow(new RuntimeException("one")).
doThrow(new RuntimeException("two"))
.when(mock).someVoidMethod();
See javadoc for Mockito.doThrow(Throwable[])
toBeThrown
- to be thrown when the stubbed method is calledStubber doThrow(Class<? extends Throwable> toBeThrown)
Mockito.doThrow(Class)
style:
doThrow(RuntimeException.class).
doThrow(IllegalArgumentException.class)
.when(mock).someVoidMethod();
See javadoc for Mockito.doThrow(Class)
toBeThrown
- exception class to be thrown when the stubbed method is calledStubber doThrow(Class<? extends Throwable> toBeThrown, Class<? extends Throwable>... nextToBeThrown)
Mockito.doThrow(Class)
style:
doThrow(RuntimeException.class).
doThrow(IllegalArgumentException.class)
.when(mock).someVoidMethod();
See javadoc for Mockito.doThrow(Class)
toBeThrown
- exception class to be thrown when the stubbed method is callednextToBeThrown
- exception class next to be thrown when the stubbed method is calledStubber doAnswer(Answer answer)
Mockito.doAnswer(Answer)
style:
doAnswer(answerOne).
doAnswer(answerTwo)
.when(mock).someVoidMethod();
See javadoc for Mockito.doAnswer(Answer)
answer
- to answer when the stubbed method is calledStubber doNothing()
Mockito.doNothing()
style:
doNothing().
doThrow(new RuntimeException("two"))
.when(mock).someVoidMethod();
See javadoc for Mockito.doNothing()
Stubber doReturn(Object toBeReturned)
Mockito.doReturn(Object)
style.
See javadoc for Mockito.doReturn(Object)
toBeReturned
- to be returned when the stubbed method is calledStubber doReturn(Object toBeReturned, Object... nextToBeReturned)
Mockito.doReturn(Object)
style.
See javadoc for Mockito.doReturn(Object, Object...)
toBeReturned
- to be returned when the stubbed method is callednextToBeReturned
- to be returned in consecutive calls when the stubbed method is calledStubber doCallRealMethod()
Mockito.doCallRealMethod()
style.
See javadoc for Mockito.doCallRealMethod()