
Durgesh Tiwari
Author
Mockito is a popular mocking framework used in Java unit testing to simulate the behavior of real objects. It helps developers test components in isolation without depending on external systems like databases, APIs, or services.
In simple words: Mockito allows developers to create “fake” objects so code can be tested without using real dependencies.
Mocking is a testing technique where real objects are replaced with fake (mock) objects that simulate behavior during testing.
Mocking is useful when:
Real objects are complex
External systems are unavailable
Isolated unit testing is required
👉 Example: Instead of connecting to a real database, a mock database object is used during testing.

Mockito is a Java framework used to:
Create mock objects
Define behavior of mocks
Verify method calls
It is widely used with JUnit for unit testing.
In real-world applications, classes often depend on databases, APIs, or external services. Mockito helps remove these dependencies during testing.
Features of Mockito
Supports isolated unit testing
Removes dependency on external systems
Makes tests faster and more reliable
Easy to integrate with JUnit and Spring Boot
Simplifies testing of service-layer components
👉 Mockito is widely used in enterprise Java and Spring Boot applications.

The following example shows how Mockito creates and uses mock objects for unit testing without using real dependencies.
This is a simple service class containing the actual business logic.
public class CalculatorService {
public int add(int a, int b) {
return a + b;
}
}In this test case, Mockito creates a mock object and defines its behavior during testing.
import static org.mockito.Mockito.*;
import static org.junit.Assert.*;
import org.junit.Test;
public class CalculatorTest {
@Test
public void testAdd() {
CalculatorService service =
mock(CalculatorService.class);
when(service.add(2, 3))
.thenReturn(5);
int result = service.add(2, 3);
assertEquals(5, result);
}
}👉 Here, the real object is replaced with a mock object during testing.
Mockito provides several useful methods for creating and managing mock objects during unit testing.
Used to create a mock object.
CalculatorService service =
mock(CalculatorService.class);Used to define the behavior of a mock object.
when(service.add(2, 3))
.thenReturn(5);
Used to check whether a method was called during testing.
verify(service).add(2, 3);
Feature | Real Object | Mock Object |
|---|---|---|
Database Usage | Connects to a real database | Does not connect to a real database |
External Dependency | Depends on APIs, services, or external systems | Works independently without external systems |
Execution Speed | Slower due to real operations | Faster because behavior is simulated |
Testing Isolation | Cannot provide complete isolation | Supports isolated unit testing |
Mockito is commonly used in:
Spring Boot applications
REST API and controller testing
Service-layer and business logic testing
Banking and financial applications
Enterprise backend systems
👉 Mockito helps developers test applications without relying on real external systems.
Mainly useful for unit testing
Cannot test complete system behavior
Excessive mocking may weaken integration testing
Mockito is a powerful framework for testing Java applications in isolation.
It helps developers simulate real objects, making unit testing faster, cleaner, and more reliable.
Commonly used with JUnit
Supports mock creation and method verification
Essential for professional Java testing workflows