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 Details

  • Method Details

    • 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:
    • 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 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.