Overview | Package | Class | Use | Tree | Deprecated | Index | Help
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD

## org.mockito.internal.stubbing
Class BaseStubbing<T>

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

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

Direct Known Subclasses:
  ConsecutiveStubbing, OngoingStubbingImpl

    public abstract class BaseStubbing<T>

extends java.lang.Object
implements OngoingStubbing<T>, DeprecatedOngoingStubbing<T>

Constructor Summary
`BaseStubbing()`

Method Summary
`OngoingStubbing<T>` | `thenCallRealMethod()` Sets the real implementation to be called when the method is called on a mock object.
`OngoingStubbing<T>` | `thenReturn(T value)` Sets a return value to be returned when the method is called.
`OngoingStubbing<T>` | `thenReturn(T value, T... values)` Sets consecutive return values to be returned when the method is called.
`OngoingStubbing<T>` | `thenThrow(java.lang.Class<? extends java.lang.Throwable>... throwableClasses)` Sets Throwable classes to be thrown when the method is called.
`OngoingStubbing<T>` | `thenThrow(java.lang.Throwable... throwables)` Sets Throwable objects to be thrown when the method is called.
`DeprecatedOngoingStubbing<T>` | `toReturn(T value)` Set a return value for the stubbed method.
`DeprecatedOngoingStubbing<T>` | `toThrow(java.lang.Throwable throwable)` Set a Throwable to be thrown when the stubbed 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
`getMock, then, thenAnswer`

Methods inherited from interface org.mockito.stubbing.DeprecatedOngoingStubbing
`toAnswer`

Constructor Detail

### BaseStubbing

    public BaseStubbing()

Method Detail

### thenReturn

    public OngoingStubbing<T> thenReturn(T value)

  Description copied from interface: `OngoingStubbing`
  Sets a return value to be returned when the method is called. E.g:

       when(mock.someMethod()).thenReturn(10);

  See examples in javadoc for `Mockito.when(T)`

  Specified by:
    `thenReturn` in interface `OngoingStubbing<T>`
  Parameters:
    `value` - return value
  Returns:
    iOngoingStubbing object that allows stubbing consecutive calls

### thenReturn

    public OngoingStubbing<T> thenReturn(T value,
                                         T... values)

  Description copied from interface: `OngoingStubbing`
  Sets consecutive return values to be returned when the method is called. E.g:

       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)`

  Specified by:
    `thenReturn` in interface `OngoingStubbing<T>`
  Parameters:
    `value` - first return value
    `values` - next return values
  Returns:
    iOngoingStubbing object that allows stubbing consecutive calls

### thenThrow

    public OngoingStubbing<T> thenThrow(java.lang.Throwable... throwables)

  Description copied from interface: `OngoingStubbing`
  Sets Throwable objects to be thrown when the method is called. E.g:

       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)`

  Specified by:
    `thenThrow` in interface `OngoingStubbing<T>`
  Parameters:
    `throwables` - to be thrown on method invocation
  Returns:
    iOngoingStubbing object that allows stubbing consecutive calls

### thenThrow

    public OngoingStubbing<T> thenThrow(java.lang.Class<? extends java.lang.Throwable>... throwableClasses)

  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 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)`

  Specified by:
    `thenThrow` in interface `OngoingStubbing<T>`
  Parameters:
    `throwableClasses` - to be thrown on method invocation
  Returns:
    iOngoingStubbing object that allows stubbing consecutive calls

### thenCallRealMethod

    public OngoingStubbing<T> 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 & 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)`

  Specified by:
    `thenCallRealMethod` in interface `OngoingStubbing<T>`
  Returns:
    iOngoingStubbing object that allows stubbing consecutive calls

### toReturn

    public DeprecatedOngoingStubbing<T> toReturn(T value)

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

       stub(mock.someMethod()).toReturn(10);

  See examples in javadoc for `Mockito.stub(T)`

  Specified by:
    `toReturn` in interface `DeprecatedOngoingStubbing<T>`
  Parameters:
    `value` - return value
  Returns:
    iOngoingStubbing object that allows stubbing consecutive calls

### toThrow

    public DeprecatedOngoingStubbing<T> toThrow(java.lang.Throwable throwable)

  Description copied from interface: `DeprecatedOngoingStubbing`
  Set a Throwable to be thrown when the stubbed method is called. E.g:

       stub(mock.someMethod()).toThrow(new RuntimeException());

  If throwable is a checked exception then it has to match one of the checked exceptions of method signature. See examples in javadoc for `Mockito.stub(T)`

  Specified by:
    `toThrow` in interface `DeprecatedOngoingStubbing<T>`
  Parameters:
    `throwable` - to be thrown on method invocation
  Returns:
    iOngoingStubbing object that allows stubbing consecutive calls

Overview | Package | Class | Use | Tree | Deprecated | Index | Help
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD