OngoingStubbingImpl
Date: 2015-09-18

org.mockito.internal.stubbing

## Class OngoingStubbingImpl<T>

- java.lang.Object
- - org.mockito.internal.stubbing.BaseStubbing<T>
  - - org.mockito.internal.stubbing.OngoingStubbingImpl<T>

- All Implemented Interfaces:
    IOngoingStubbing, DeprecatedOngoingStubbing<T>, OngoingStubbing<T>

      public class OngoingStubbingImpl<T>
      extends BaseStubbing<T>

- - ### Constructor Summary

    Constructors
    Constructor and Description
    `OngoingStubbingImpl(InvocationContainerImpl invocationContainerImpl)`

  - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `<M> M` | `getMock()` Returns the mock that was used for this stub.
    `java.util.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.

    - ### Methods inherited from class org.mockito.internal.stubbing.BaseStubbing

      `thenCallRealMethod, thenReturn, thenReturn, thenThrow, thenThrow, toReturn, toThrow`

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Constructor Detail

    - #### OngoingStubbingImpl

          public OngoingStubbingImpl(InvocationContainerImpl invocationContainerImpl)

  - ### Method Detail

    - #### thenAnswer

          public OngoingStubbing<T> thenAnswer(Answer<?> answer)

      Description copied from interface: `OngoingStubbing`
      Sets a generic Answer for the method. E.g:

           when(mock.someMethod(10)).thenAnswer(new Answer<Integer>() {
               public Integer answer(InvocationOnMock invocation) throws Throwable {
                   return (Integer) invocation.getArguments()[0];
               }
           }

      Parameters:
        `answer` - the custom answer to execute.
      Returns:
        iOngoingStubbing object that allows stubbing consecutive calls

    - #### then

          public OngoingStubbing<T> then(Answer<?> answer)

      Description copied from interface: `OngoingStubbing`
      Sets a generic Answer for the method. This method is an alias of `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());

      Parameters:
        `answer` - the custom answer to execute.
      Returns:
        iOngoingStubbing object that allows stubbing consecutive calls
      See Also:
        `OngoingStubbing.thenAnswer(Answer)`

    - #### toAnswer

          public DeprecatedOngoingStubbing<T> toAnswer(Answer<?> answer)

      Description copied from interface: `DeprecatedOngoingStubbing`
      Set a generic Answer for the stubbed method. E.g:

           stub(mock.someMethod(10)).toAnswer(new Answer<Integer>() {
               public Integer answer(InvocationOnMock invocation) throws Throwable {
                   return (Integer) invocation.getArguments()[0];
               }
           }

      Parameters:
        `answer` - the custom answer to execute.
      Returns:
        iOngoingStubbing object that allows stubbing consecutive calls

    - #### getRegisteredInvocations

          public java.util.List<Invocation> getRegisteredInvocations()

    - #### 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 & 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... {}

      Type Parameters:
        `M` - The mock type given by the variable type.
      Returns:
        Mock used in this ongoing stubbing.