Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Usages of doThrow() doAnswer() doNothing() and doReturn() in mockito

Tags:

java

mockito

I was learning mockito and I understood the basic usages of the above mentioned functions from the link.

But I would like to know whether it can be used for any other cases?

like image 433
User666 Avatar asked Mar 03 '15 16:03

User666


People also ask

What does Mockito doThrow do?

Mockito provides following methods that can be used to mock void methods. doAnswer() : We can use this to perform some operations when a mocked object method is called that is returning void. doThrow() : We can use doThrow() when we want to stub a void method that throws exception.

What is the use of doNothing in Mockito?

Mockito 's doNothing() is used when you want to test void methods because void methods do not return anything so there is no way you can verify using assert. These void methods may be anywhere, for example, in service class, in dao class, etc.

What does doThrow do?

doThrow : Basically used when you want to throw an exception when a method is being called within a mock object.

What is doAnswer Mockito?

Answer is used when you need to do additional actions when a mocked method is invoked, e.g. when you need to compute the return value based on the parameters of this method call. Use doAnswer() when you want to stub a void method with generic Answer .


1 Answers

doThrow : Basically used when you want to throw an exception when a method is being called within a mock object.

public void validateEntity(final Object object){} Mockito.doThrow(IllegalArgumentException.class) .when(validationService).validateEntity(Matchers.any(AnyObjectClass.class)); 

doReturn : Used when you want to send back a return value when a method is executed.

public Socket getCosmosSocket() throws IOException {} Mockito.doReturn(cosmosSocket).when(cosmosServiceImpl).getCosmosSocket(); 

doAnswer: Sometimes you need to do some actions with the arguments that are passed to the method, for example, add some values, make some calculations or even modify them doAnswer gives you the Answer<?> interface that being executed in the moment that method is called, this interface allows you to interact with the parameters via the InvocationOnMock argument. Also, the return value of answer method will be the return value of the mocked method.

public ReturnValueObject quickChange(Object1 object); Mockito.doAnswer(new Answer<ReturnValueObject>() {          @Override         public ReturnValueObject answer(final InvocationOnMock invocation) throws Throwable {              final Object1 originalArgument = (invocation.getArguments())[0];             final ReturnValueObject returnedValue = new ReturnValueObject();             returnedValue.setCost(new Cost());              return returnedValue ;         } }).when(priceChangeRequestService).quickCharge(Matchers.any(Object1.class)); 

doNothing: (From documentation) Use doNothing() for setting void methods to do nothing. Beware that void methods on mocks do nothing by default! However, there are rare situations when doNothing() comes handy:

  • Stubbing consecutive calls on a void method:

    doNothing(). doThrow(new RuntimeException()) .when(mock).someVoidMethod();  //does nothing the first time: mock.someVoidMethod();  //throws RuntimeException the next time: mock.someVoidMethod(); 
  • When you spy real objects and you want the void method to do nothing:

    List list = new LinkedList(); List spy = spy(list);  //let's make clear() do nothing doNothing().when(spy).clear();  spy.add("one");  //clear() does nothing, so the list still contains "one" spy.clear(); 
like image 154
Koitoer Avatar answered Oct 19 '22 08:10

Koitoer