In Mockito, the most common way to create a mock object is to use either @Mock annotation or Mockito.mock method. When a method having a return value is called for a mock object, it returns an empty value such as 0, empty collections, an empty string, and null.
When you want to change a return value for testing, you can do it with when-return. This works well if the object’s behavior is simple. However, there could be cumbersome work to write multiple when-returns if a mock object is too complex.
For those cases, Mockito supports a way to set logic for specifying what to return instead of an empty value. You can do it by implementing Answer interface and passing it when you mock an object. There are predefined Answers for common cases in Mockito. You could utilize this rather than implementing a custom Answer. Here are some useful Mockito Answers.
RETURNS_MOCKS
If a method return type is a custom class, a mock returns null because there is no empty value for a custom class. RETURN_MOCKS will try to return mocks if possible instead of null. Since final class cannot be mocked, null is still returned in that case.
A below example verifies a behavior by changing the getTitle(). In onDataLoaded(), the presenter accesses getImage().getUrl() to show an image. Although getTitle() is a main condition to verify, there is an additional when-return to avoid a null pointer exception.
With RETURNS_MOCKS, this test could be simplified. You don’t need to set up additional when-return since a mock is returned.
When you use an annotation, you can specify Answer like below.
RETURNS_SELF
This uses the return type of a method. If that type equals to the class or a superclass, it will return the mock. This is especially useful for builder classes. You don’t need to implement when-return for builder methods.
You can see there are two when-returns to mock a builder properly.
When RETURNS_SELF is set, the mock is returned when it is assignable. Thus, you don’t need to set up manually. Note that the mock is returned when the return type is Object. Object is a superclass of the class.
RETURNS_DEEP_STUBS
When a class refers to another class, you sometimes have a chain of methods to get a value. This chain could be deep and multiple levels. For mocking them, you need to return a mock which returns a mock. For example, there are several when-returns because of a deep chain.
RETURNS_DEEP_STUBS makes it possible to set up a return value for a chain of methods without setting mocks between a chain. Thus, you can shortly set it like below.
Conclusion
Mockito Answer supports a custom logic to return a value instead of a literal value. If you have multiple when-thens before verifying, consider using predefined Mockito Answers. Otherwise, you can create your own Answer to simplify setup and focus on verification.