Mocking Dependencies and Collaborators in Unit Tests with Mockito

Unit testing is a crucial part of software development, ensuring that each isolated component of the code functions as expected. However, when writing unit tests, we often encounter dependencies and collaborators that make testing more challenging.

Mocks come to the rescue by allowing us to simulate the behavior of these dependencies and collaborators. Mockito, a powerful mocking framework for Java, provides an easy and flexible way to mock objects and simplify unit testing.

What is a Mock?

In the context of software testing, a mock is an object that simulates the behavior of a real object. By creating a mock, we can define how the mock should behave during a test case, allowing us to isolate the code being tested.

For example, consider a case where we have a Service class that depends on a Database class for data retrieval. When writing unit tests for the Service class, we do not want to rely on the actual database connection. Instead, we can create a mock of the Database class that returns predefined data or behaves in a specific way, making our tests more self-contained and predictable.

Getting Started with Mockito

To start using Mockito, we need to include the Mockito framework in our project's dependencies. The most common way is to add the following dependency to our project's build file:


With the framework added, we can now utilize Mockito to create mocks and define their behavior within our unit tests.

Creating Mock Objects

To create a mock object with Mockito, we need to use the Mockito.mock() method and pass the class or interface we want to mock as an argument. This method returns an instance of the desired class/interface, but with all its methods overridden so that they do nothing or return default values.

Here's an example of how we can create a mock ProductDao object:

ProductDao mockProductDao = Mockito.mock(ProductDao.class);

Now, we can treat mockProductDao as a valid instance of ProductDao and define its behavior based on our test requirements.

Setting Return Values

One of the most common use cases of mocking is setting return values for mocked methods. We can use Mockito's when().thenReturn() syntax to specify the return value for a method call on a mock object.

For instance, let's say our ProductDao has a getProductById() method. We can define its behavior using Mockito like this:

Product expectedProduct = new Product("123", "Mocked Product");

In this example, whenever getProductById("123") is called on the mockProductDao, it will return expectedProduct. This allows us to control the behavior of the mock and ensure consistent results for our tests.

Verifying Method Invocations

In addition to setting return values, Mockito enables us to verify that certain methods were called on our mock objects during our test execution. This helps ensure that the code under test interacts correctly with its dependencies.

To verify method invocations, we use the verify() method provided by Mockito. For instance, let's say our Service class calls the saveProduct() method on the ProductDao when processing some logic. We can verify that this method was called using the following code:

service.saveProduct(new Product("123", "Test Product"));

The verify() method checks if the specified method was called on the mock object. In this example, we verify that the saveProduct() method was invoked with any instance of Product. We can also specify the number of invocations or the order of method calls for more advanced verifications.


Mocking dependencies and collaborators in unit tests is essential for isolating the code being tested and making the tests more reliable and predictable. Mockito simplifies the mocking process by providing an intuitive API for creating and configuring mock objects.

By leveraging Mockito's capabilities, we can effectively simulate the behavior of dependencies, set return values for methods, and verify method invocations. This allows us to write focused and reliable unit tests that thoroughly exercise our code while maintaining a controlled environment.

So why wait? Start using Mockito and supercharge your unit tests with powerful mocking capabilities!

© NoobToMaster - A 10xcoder company