What are the usages of `doThrow()`, `doAnswer()`, `doNothing()`, and `doReturn()` in Mockito?

What are the usages of doThrow(), doAnswer(), doNothing(), and doReturn() in Mockito?

I have learned the basic usage of these methods in Mockito, but I am curious to know if they can be applied in other scenarios. Can someone provide insights into different use cases, especially for doNothing() in Mockito?

doThrow() is used when you want to specify that a method should throw an exception when it is called. This is typically used in situations where you are mocking methods that are expected to throw exceptions.

When you want to simulate an exception being thrown by a method call, such as an error in a database query or network request.

// Example: Simulating an exception being thrown doThrow(new RuntimeException("Database down")).when(mockedDatabase).fetchData();

This tells Mockito that when fetchData() is called on mockedDatabase, it will throw a RuntimeException.

doAnswer() allows you to define a custom action when a mocked method is called. It’s useful when you want to execute some logic (e.g., return a dynamic value, print something, or perform some side effect) when a method is called, instead of just returning a fixed value.

Use Case Example: Simulating complex behavior like modifying arguments, returning different values based on arguments, or logging something during the test.

// Example: Custom action with doAnswer

doAnswer(invocation -> {
    Object[] args = invocation.getArguments();
    System.out.println("Method called with: " + Arrays.toString(args));
    return "Custom result";  // return a custom result
}).when(mockedService).processData(any());

This will log the arguments passed to processData and return a custom result.

doNothing() is commonly used when you want to mock methods with void return types and make sure that no action is performed when the method is called. It is useful when you are mocking methods that modify state but you do not want them to do anything in the test.

When you need to mock a method that has a side effect but don’t want to actually execute that side effect during testing (e.g., logging, sending an email).

// Example: Void method that does nothing
doNothing().when(mockedService).sendEmail(anyString());
In this case, even if sendEmail is called in the code under test, it won't actually send an email during testing.

doReturn()

is an alternative to when() that is used to stub methods. It’s often used in situations where when() doesn’t work as expected, especially when mocking methods with final classes or methods (which can’t be stubbed using when() due to limitations in mocking static or final methods).

Use Case Example: When mocking methods of final classes or methods that return non-void types.

// Example: Stubbing method that returns a value
doReturn("Mocked Response").when(mockedService).fetchData();

Here, fetchData() will return “Mocked Response” when it’s called, instead of actually performing the logic inside the method.