T - public class DelegatingToConstructorsOngoingStubbing<T>
extends java.lang.Object
implements org.mockito.stubbing.OngoingStubbing<T>
| Constructor and Description |
|---|
DelegatingToConstructorsOngoingStubbing(java.lang.reflect.Constructor<?>[] ctors,
org.mockito.stubbing.OngoingStubbing<T> stubbing) |
| Modifier and Type | Method and Description |
|---|---|
<M> M |
getMock()
Returns the mock that was used for this stub.
|
org.mockito.stubbing.OngoingStubbing<T> |
then(org.mockito.stubbing.Answer<?> answer)
Sets a generic Answer for the method.
|
org.mockito.stubbing.OngoingStubbing<T> |
thenAnswer(org.mockito.stubbing.Answer<?> answer)
Sets a generic Answer for the method.
|
org.mockito.stubbing.OngoingStubbing<T> |
thenCallRealMethod()
Sets the real implementation to be called when the method is called on a mock object.
|
org.mockito.stubbing.OngoingStubbing<T> |
thenReturn(T value)
Sets a return value to be returned when the method is called.
|
org.mockito.stubbing.OngoingStubbing<T> |
thenReturn(T value,
T... values)
Sets consecutive return values to be returned when the method is called.
|
org.mockito.stubbing.OngoingStubbing<T> |
thenThrow(java.lang.Class<? extends java.lang.Throwable>... throwableClasses)
Sets Throwable classes to be thrown when the method is called.
|
org.mockito.stubbing.OngoingStubbing<T> |
thenThrow(java.lang.Throwable... throwables)
Sets Throwable objects to be thrown when the method is called.
|
public DelegatingToConstructorsOngoingStubbing(java.lang.reflect.Constructor<?>[] ctors,
org.mockito.stubbing.OngoingStubbing<T> stubbing)
public org.mockito.stubbing.OngoingStubbing<T> thenReturn(T value)
org.mockito.stubbing.OngoingStubbing
when(mock.someMethod()).thenReturn(10);
See examples in javadoc for Mockito.when(T)thenReturn in interface org.mockito.stubbing.OngoingStubbing<T>value - return valuepublic org.mockito.stubbing.OngoingStubbing<T> thenReturn(T value, T... values)
org.mockito.stubbing.OngoingStubbing
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)
thenReturn in interface org.mockito.stubbing.OngoingStubbing<T>value - first return valuevalues - next return valuespublic org.mockito.stubbing.OngoingStubbing<T> thenThrow(java.lang.Throwable... throwables)
org.mockito.stubbing.OngoingStubbing
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)
thenThrow in interface org.mockito.stubbing.OngoingStubbing<T>throwables - to be thrown on method invocationpublic org.mockito.stubbing.OngoingStubbing<T> thenThrow(java.lang.Class<? extends java.lang.Throwable>... throwableClasses)
org.mockito.stubbing.OngoingStubbing
when(mock.someMethod()).thenThrow(RuntimeException.class);
Each throwable class will be instantiated for each method invocation.
If throwableClasses contain a checked exception then it has to match one of the checked exceptions of method signature.
You can specify throwableClasses 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)
thenThrow in interface org.mockito.stubbing.OngoingStubbing<T>throwableClasses - to be thrown on method invocationpublic org.mockito.stubbing.OngoingStubbing<T> thenCallRealMethod()
org.mockito.stubbing.OngoingStubbingAs 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 & well-designed code.
// 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();
See also javadoc 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)
thenCallRealMethod in interface org.mockito.stubbing.OngoingStubbing<T>public org.mockito.stubbing.OngoingStubbing<T> thenAnswer(org.mockito.stubbing.Answer<?> answer)
org.mockito.stubbing.OngoingStubbing
when(mock.someMethod(10)).thenAnswer(new Answer<Integer>() {
public Integer answer(InvocationOnMock invocation) throws Throwable {
return (Integer) invocation.getArguments()[0];
}
}
thenAnswer in interface org.mockito.stubbing.OngoingStubbing<T>answer - the custom answer to execute.public org.mockito.stubbing.OngoingStubbing<T> then(org.mockito.stubbing.Answer<?> answer)
org.mockito.stubbing.OngoingStubbingOngoingStubbing.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());
then in interface org.mockito.stubbing.OngoingStubbing<T>answer - the custom answer to execute.OngoingStubbing.thenAnswer(Answer)public <M> M getMock()
org.mockito.stubbing.OngoingStubbingIt 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 & 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... {}
getMock in interface org.mockito.stubbing.OngoingStubbing<T>M - The mock type given by the variable type.