Class BaseStubbing<T>
- All Implemented Interfaces:
OngoingStubbing<T>
- Direct Known Subclasses:
ConsecutiveStubbing
,OngoingStubbingImpl
-
Method Summary
Modifier and TypeMethodDescription<M> M
getMock()
Returns the mock that was used for this stub.Sets a generic Answer for the method.Sets the real implementation to be called when the method is called on a mock object.thenReturn
(T value) Sets a return value to be returned when the method is called.thenReturn
(T value, T... values) Sets consecutive return values to be returned when the method is called.Sets a Throwable type to be thrown when the method is called.Sets Throwable classes to be thrown when the method is called.Sets Throwable objects to be thrown when the method is called.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface org.mockito.stubbing.OngoingStubbing
thenAnswer
-
Method Details
-
then
Description copied from interface:OngoingStubbing
Sets a generic Answer for the method. This method is an alias ofOngoingStubbing.thenAnswer(Answer)
. This alias allows more readable tests on occasion, for example://using 'then' alias: when(mock.foo()).then(returnCoolValue()); //versus good old 'thenAnswer: when(mock.foo()).thenAnswer(byReturningCoolValue());
- Specified by:
then
in interfaceOngoingStubbing<T>
- Parameters:
answer
- the custom answer to execute.- Returns:
- object that allows stubbing consecutive calls
- See Also:
-
thenReturn
Description copied from interface:OngoingStubbing
Sets a return value to be returned when the method is called. E.g:
See examples in javadoc forwhen(mock.someMethod()).thenReturn(10);
Mockito.when(T)
- Specified by:
thenReturn
in interfaceOngoingStubbing<T>
- Parameters:
value
- return value- Returns:
- object that allows stubbing consecutive calls
-
thenReturn
Description copied from interface:OngoingStubbing
Sets consecutive return values to be returned when the method is called. E.g:
Last return value in the sequence (in example: 3) determines the behavior of further consecutive calls.when(mock.someMethod()).thenReturn(1, 2, 3);
See examples in javadoc for
Mockito.when(T)
- Specified by:
thenReturn
in interfaceOngoingStubbing<T>
- Parameters:
value
- first return valuevalues
- next return values- Returns:
- object that allows stubbing consecutive calls
-
thenThrow
Description copied from interface:OngoingStubbing
Sets Throwable objects to be thrown when the method is called. E.g:
If throwables contain a checked exception then it has to match one of the checked exceptions of method signature.when(mock.someMethod()).thenThrow(new RuntimeException());
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)
- Specified by:
thenThrow
in interfaceOngoingStubbing<T>
- Parameters:
throwables
- to be thrown on method invocation- Returns:
- object that allows stubbing consecutive calls
-
thenThrow
Description copied from interface:OngoingStubbing
Sets a Throwable type to be thrown when the method is called. E.g:when(mock.someMethod()).thenThrow(RuntimeException.class);
If the throwable class is a checked exception then it has to match one of the checked exceptions of the stubbed method signature.
If throwable is null then exception will be thrown.
See examples in javadoc for
Mockito.when(T)
Note depending on the JVM, stack trace information may not be available in the generated throwable instance. If you require stack trace information, use
OngoingStubbing.thenThrow(Throwable...)
instead.- Specified by:
thenThrow
in interfaceOngoingStubbing<T>
- Parameters:
throwableType
- to be thrown on method invocation- Returns:
- object that allows stubbing consecutive calls
-
thenThrow
public OngoingStubbing<T> thenThrow(Class<? extends Throwable> toBeThrown, Class<? extends Throwable>... nextToBeThrown) Description copied from interface:OngoingStubbing
Sets Throwable classes to be thrown when the method is called. E.g:when(mock.someMethod()).thenThrow(RuntimeException.class);
Each throwable class will be instantiated for each method invocation.
If
throwableTypes
contain a checked exception then it has to match one of the checked exceptions of method signature.You can specify
throwableTypes
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)
Note since JDK 7, invoking this method will raise a compiler warning "possible heap pollution", this API is safe to use. If you don't want to see this warning it is possible to chain
OngoingStubbing.thenThrow(Class)
Note depending on the JVM, stack trace information may not be available in the generated throwable instance. If you require stack trace information, use
OngoingStubbing.thenThrow(Throwable...)
instead.- Specified by:
thenThrow
in interfaceOngoingStubbing<T>
- Parameters:
toBeThrown
- to be thrown on method invocationnextToBeThrown
- next to be thrown on method invocation- Returns:
- object that allows stubbing consecutive calls
-
thenCallRealMethod
Description copied from interface:OngoingStubbing
Sets the real implementation to be called when the method is called on a mock object.As usual you are going to read the partial mock warning: Object oriented programming is more less tackling complexity by dividing the complexity into separate, specific, SRPy objects. How does partial mock fit into this paradigm? Well, it just doesn't... Partial mock usually means that the complexity has been moved to a different method on the same object. In most cases, this is not the way you want to design your application.
However, there are rare cases when partial mocks come handy: dealing with code you cannot change easily (3rd party interfaces, interim refactoring of legacy code etc.) However, I wouldn't use partial mocks for new, test-driven and well-designed code.
See also javadoc// someMethod() must be safe (e.g. doesn't throw, doesn't have dependencies to the object state, etc.) // if it isn't safe then you will have trouble stubbing it using this api. Use Mockito.doCallRealMethod() instead. when(mock.someMethod()).thenCallRealMethod(); // calls real method: mock.someMethod();
Mockito.spy(Object)
to find out more about partial mocks. Mockito.spy() is a recommended way of creating partial mocks. The reason is it guarantees real methods are called against correctly constructed object because you're responsible for constructing the object passed to spy() method.See examples in javadoc for
Mockito.when(T)
- Specified by:
thenCallRealMethod
in interfaceOngoingStubbing<T>
- Returns:
- object that allows stubbing consecutive calls
-
getMock
public <M> M getMock()Description copied from interface:OngoingStubbing
Returns the mock that was used for this stub.It allows to create a stub in one line of code. This can be helpful to keep test code clean. For example, some boring stub can be created and stubbed at field initialization in a test:
public class CarTest { Car boringStubbedCar = when(mock(Car.class).shiftGear()).thenThrow(EngineNotStarted.class).getMock(); @Test public void should... {}
- Specified by:
getMock
in interfaceOngoingStubbing<T>
- Type Parameters:
M
- The mock type given by the variable type.- Returns:
- Mock used in this ongoing stubbing.
-