Mockito

Mocking framework for unit tests in Java

Jay Patel
2 min readFeb 12, 2022

Introduction to Mockito

•Mockito is a mocking framework.

• It is JAVA-based library that is used for effective unit testing of JAVA

applications.

• Mockito is used to mock interfaces so that a dummy functionality can

be added to a mock interface that can be used in unit testing.

Explanation with a real-world example:-

1. We must have seen Google Maps showing the Uber/Ola booking screen.

2. It is clear that Google Maps uses Uber/Ola APIs to provide this information on Google Maps.

From the above example we could infer that :

1. So in a large system where each piece of functionality depends on

another peace, it becomes difficult to test just one part in isolation, that is

where mockito is used.

2. Mockito can be used to imitate the presence of these dependencies

so that we can unit test our code in isolation from other parts of the system.

Use Case:

•How can Google Maps test their code effectively? Issuing 1000s

of calls to Uber/Ola APIs every time a Google developer tests their

code is not ideal:

• Slow unit tests because now it involves network calls to Uber/Ola servers

• If any of these APIs are rate-limited or charged by Uber/Ola, then

running unit tests become costly

• If Uber/Ola’s API servers are temporarily down, Google Maps cannot be

unit tested!

•This is where Test Mocks come handy — Google Maps can create Test Mocks

for Uber/Ola API services mimicking their behavior and so unit tests can

use them instead of the actual API calls.

Mockito is just a Java framework that will help you create test mocks.

--

--