public class OngoingStubbingImpl<T> extends BaseStubbing<T>
Constructor and Description |
---|
OngoingStubbingImpl(InvocationContainerImpl invocationContainerImpl) |
Modifier and Type | Method and Description |
---|---|
<M> M |
getMock()
Returns the mock that was used for this stub.
|
List<Invocation> |
getRegisteredInvocations() |
OngoingStubbing<T> |
then(Answer<?> answer)
Sets a generic Answer for the method.
|
OngoingStubbing<T> |
thenAnswer(Answer<?> answer)
Sets a generic Answer for the method.
|
DeprecatedOngoingStubbing<T> |
toAnswer(Answer<?> answer)
Set a generic Answer for the stubbed method.
|
thenCallRealMethod, thenReturn, thenReturn, thenThrow, thenThrow, toReturn, toThrow
public OngoingStubbingImpl(InvocationContainerImpl invocationContainerImpl)
public OngoingStubbing<T> thenAnswer(Answer<?> answer)
OngoingStubbing
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.public OngoingStubbing<T> then(Answer<?> answer)
OngoingStubbing
OngoingStubbing.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());
answer
- the custom answer to execute.OngoingStubbing.thenAnswer(Answer)
public DeprecatedOngoingStubbing<T> toAnswer(Answer<?> answer)
DeprecatedOngoingStubbing
stub(mock.someMethod(10)).toAnswer(new Answer<Integer>() {
public Integer answer(InvocationOnMock invocation) throws Throwable {
return (Integer) invocation.getArguments()[0];
}
}
answer
- the custom answer to execute.public List<Invocation> getRegisteredInvocations()
public <M> M getMock()
OngoingStubbing
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 & 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... {}
M
- The mock type given by the variable type.