Interface Stubber

All Known Implementing Classes:
StubberImpl

public interface Stubber
Allows to choose a method when stubbing in doThrow()|doAnswer()|doNothing()|doReturn() style

Example:


   doThrow(new RuntimeException()).when(mockedList).clear();
   
   //following throws RuntimeException:
   mockedList.clear();
 
Also useful when stubbing consecutive calls:

   doThrow(new RuntimeException("one")).
   doThrow(new RuntimeException("two"))
   .when(mock).someVoidMethod();
 
Read more about those methods:

Mockito.doThrow(Throwable)

Mockito.doAnswer(Answer)

Mockito.doNothing()

Mockito.doReturn(Object)

See examples in javadoc for Mockito

  • Method Details

    • when

      <T> T when(T mock)
      Allows to choose a method when stubbing in doThrow()|doAnswer()|doNothing()|doReturn() style

      Example:

      
         doThrow(new RuntimeException())
         .when(mockedList).clear();
         
         //following throws RuntimeException:
         mockedList.clear();
       
      Read more about those methods:

      Mockito.doThrow(Throwable)

      Mockito.doAnswer(Answer)

      Mockito.doNothing()

      Mockito.doReturn(Object)

      See examples in javadoc for Mockito

      Parameters:
      mock - The mock
      Returns:
      select method for stubbing
    • doThrow

      Stubber doThrow(Throwable toBeThrown)
      Use it for stubbing consecutive calls in Mockito.doThrow(Throwable) style:
      
         doThrow(new RuntimeException("one")).
         doThrow(new RuntimeException("two"))
         .when(mock).someVoidMethod();
       
      See javadoc for Mockito.doThrow(Throwable)
      Parameters:
      toBeThrown - to be thrown when the stubbed method is called
      Returns:
      stubber - to select a method for stubbing
    • doThrow

      Stubber doThrow(Class<? extends Throwable> toBeThrown)
      Use it for stubbing consecutive calls in Mockito.doThrow(Class) style:
      
         doThrow(RuntimeException.class).
         doThrow(IllegalArgumentException.class)
         .when(mock).someVoidMethod();
       
      See javadoc for Mockito.doThrow(Class)
      Parameters:
      toBeThrown - exception class to be thrown when the stubbed method is called
      Returns:
      stubber - to select a method for stubbing
    • doAnswer

      Stubber doAnswer(Answer answer)
      Use it for stubbing consecutive calls in Mockito.doAnswer(Answer) style:
      
         doAnswer(answerOne).
         doAnswer(answerTwo)
         .when(mock).someVoidMethod();
       
      See javadoc for Mockito.doAnswer(Answer)
      Parameters:
      answer - to answer when the stubbed method is called
      Returns:
      stubber - to select a method for stubbing
    • doNothing

      Stubber doNothing()
      Use it for stubbing consecutive calls in Mockito.doNothing() style:
      
         doNothing().
         doThrow(new RuntimeException("two"))
         .when(mock).someVoidMethod();
       
      See javadoc for Mockito.doNothing()
      Returns:
      stubber - to select a method for stubbing
    • doReturn

      Stubber doReturn(Object toBeReturned)
      Use it for stubbing consecutive calls in Mockito.doReturn(Object) style.

      See javadoc for Mockito.doReturn(Object)

      Parameters:
      toBeReturned - to be returned when the stubbed method is called
      Returns:
      stubber - to select a method for stubbing
    • doCallRealMethod

      Stubber doCallRealMethod()
      Use it for stubbing consecutive calls in Mockito.doCallRealMethod() style.

      See javadoc for Mockito.doCallRealMethod()

      Returns:
      stubber - to select a method for stubbing