Tutorial Playlist
Unit testing is a software testing method where individual units or components of an application are tested in isolation. The purpose is to validate that each unit functions as intended. Unit testing has numerous benefits, like it improves code quality, catches bugs early in development, allows safe refactoring of code and provides documentation of code. However, some units may have dependencies on other objects and components. This makes unit testing more difficult. This is where mocking comes in. This Mockito tutorial covers it in detail.
Mockito is one of the most popular mocking frameworks for Java. In this Mockito tutorial, we'll learn the basics of Mockito, its useful features and best practices. We will make the learning process easy with Mockito examples.
By the end of this Mockito tutorial, you'll understand how to use Mockito to unit test your Java code effectively.
Mockito is an open-source mocking framework for Java, commonly referred to as the Mockito framework. It was originally created by Szczepan Faber and Philipp Haselwandter as a fork of EasyMock. Mockito simulates the behavior of real objects in automated unit tests.
This comprehensive Mockito JUnit tutorial details how Mockito helps to effectively test a code, especially when integrating with tools like Mockito Maven and Mockito Spring Boot.
In summary, Mockito provides a simple yet powerful API for configuring mock objects and leveraging them in unit tests.
Before jumping into Mockito, let's briefly discuss unit testing. JUnit is the most common Java unit testing framework. Mockito vs. JUnit is often a topic of discussion. Here is a simple unit test written with JUnit:
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class CalculatorTest {
 @Test
 public void testAdd() {
  Calculator calculator = new Calculator();
  int sum = calculator.add(2, 3);
  assertEquals(5, sum);
 }
}
This test validates the add() method of a Calculator class by:
JUnit provides the annotations and assertions to write and run unit tests quickly. However, if the Calculator class had dependencies, they would need to be handled for true unit testing. This is where Mockito comes in.
Mocking involves creating fake implementations of dependencies and injecting them into the code under test. In a real application, classes often depend on other classes and interfaces. For example, consider a UserService class that depends on a UserRepository interface:
public class UserService {
 private UserRepository userRepository;
 public UserService(UserRepository userRepository) {
  this.userRepository = userRepository;
 }
 public void register(String email, String password) {
  User user = userRepository.findByEmail(email);
  if (user != null) {
   throw new RuntimeException("Email already exists");
  }
  // Save user
 }
}
The UserService depends on the UserRepository to check if a user email already exists when registering a new account.
To properly unit test the register() method in isolation, we need to provide a mock UserRepository somehow. This allows us to simulate test scenarios like:
Without mocking, unit testing the UserService would require integration with a real UserRepository implementation like a database. That leads to slower and more brittle tests.
Let's explore some key Mockito concepts.
A mock object is a fake implementation of a real interface or class that allows predefined output to simulated method calls. Mockito allows mocking interfaces to inject a fake implementation for testing.
UserRepository mockUserRepository = Mockito.mock(UserRepository.class);
Real implementations are avoided so that tests run quickly in isolation without external dependencies.
Once mock objects are created, they need to be injected into the code under test. This allows replacing real dependencies with mocks so that their behavior can be controlled.
@TestÂ
public void registerNewUserTest() {
 UserRepository mockUserRepo = Mockito.mock(UserRepository.class);Â
 Â
 UserService userService = new UserService(mockUserRepo);
 Â
 // ...
}
Stubbing a method configures it to return a specific value when called during a test:
Mockito.when(mockUserRepo.findByEmail(anyString())).thenReturn(null);
The stub causes findByEmail() to always return null. This is useful for simulating a new user registration scenario.
Mockito verifies that methods were called with expected parameters:
Mockito.verify(mockUserRepo).findByEmail("test@example.com");
This asserts that findByEmail() was called during the test with the email "test@example.com".
There are many advantages of using Mockito for mocks in JUnit tests:
In summary, Mockito makes unit testing Java applications faster, easier and more robust.
Let's explore Mockito syntax and APIs. The key static methods of the Mockito class include:
Here is an example mock:
// Import Mockito library
import static org.mockito.Mockito.*;
// Create mockÂ
UserRepository mockUserRepo = mock(UserRepository.class);
// Stub method
when(mockUserRepo.findByEmail("test@example.com")).thenReturn(null);Â
// Use mock in test
UserService userService = new UserService(mockUserRepo);
userService.register("test@example.com", "pwd");
// Verify interaction
verify(mockUserRepo).findByEmail("test@example.com");
This demonstrates the essential Mockito workflow:
Now, let's explore some key Mockito capabilities in more detail.
Mockito allows the mocking of both classes and interfaces.
To mock an interface:
List mockList = mock(List.class);
To mock a class:
LinkedList mockList = mock(LinkedList.class);
The mocked instances do not contain any actual code implementation. They can be injected and stubbed as needed in tests.
Mockito includes a variety of matchers to verify that methods were called with the correct parameters.
For example:
//Verify with exact string
verify(mockList).add("Hello");
//Verify with any string  Â
verify(mockList).add(anyString());
//Verify with contains string
verify(mockList).add(contains("World"));
Common matchers include anyInt(), eq(), contains() and more. They provide flexibility in verifying parameter values.
The Mockito when() method allows stubbing a mock method to return a specified value:
when(mockList.size()).thenReturn(5);
int size = mockList.size(); //Returns 5
This configures all calls to mockList.size() to return 5 without a real implementation.
Exceptions can be thrown from stubbed methods via doThrow():
doThrow(new RuntimeException()).when(mockList).clear();
mockList.clear(); //Throws RuntimeException
This can be useful for testing exception-handling logic.
Interactions with mock objects can be verified using verify():
verify(mockList).add("Test");
verify(mockList, times(2)).add(anyString());
This validates that add() was called with expected parameters at least once or a specific number of times.
By default, Mockito verifies mocks in any order. The InOrder API allows verifying sequential interactions:
InOrder inOrder = inOrder(mock1, mock2);
inOrder.verify(mock1).method1();Â
inOrder.verify(mock2).method2();
This asserts that method1() was called before method2().
Mockito provides annotations for simplifying mocks and stubs. For those working with Spring, Mockito spring boot integration can be particularly useful:
@MockÂ
List mockList;
@InjectMocks
MyClassUnderTest instance;Â
@Test
public void myTest() {
 // MockList injected into instance
}
The @Mock annotation auto-creates a mock while @InjectMocks injects it into the test instance.
Here are some best practices for Mockito:
Following these practices results in focused, maintainable and robust unit tests.
Let's look at a complete example of using Mockito for a JUnit test.
We'll test a MessageService that uses a NotificationGateway interface:
public class MessageService {
 private NotificationGateway notificationGateway;
 public MessageService(NotificationGateway notificationGateway) {
  this.notificationGateway = notificationGateway;
 }
 public void sendMessage(String message) {
  // Send message
  notificationGateway.send(message);
 }
} Â
public interface NotificationGateway {
 void send(String message);
}
Here is how this can be unit-tested with mocks:
@Test
public void testSendMessage() {
 //Create mock
 NotificationGateway gateway = Mockito.mock(NotificationGateway.class);Â
 Â
 //Create service with mock
 MessageService service = new MessageService(gateway);
 //Call method under test
 service.sendMessage("Hello World");Â
 Â
 //Verify interaction with mock
 verify(gateway).send("Hello World");
 Â
}
Key steps:
This shows how mocks allow us to unit test MessageService in isolation without coupling it to any messaging infrastructure during the test.
Some examples of what you can test using Mockito:
In summary, Mockito makes tests clean and fast by removing external dependencies. Mocks improve test coverage, increase the speed and reliability of tests, and the design of code.
This Mockito documentation is a valuable resource for students to get more detailed insights on Mockito. We have covered the rationale behind mocking in unit tests, the method to create, inject and configure mocks, useful Mockito features, mocking best practices and a full Mockito example. Mockito removes dependencies that make testing difficult and slows down development. It promotes good design principles and test-driven development. Happy mocking with Mockito!
1. What are some key facts about Mockito?
Key things to know about Mockito are:
2. What are some of the key features and capabilities of Mockito?
Some of the features and capabilities of Mockito include:
3. What is the difference between @Mock and @InjectMocks?
The @Mock annotation is used to create a mock. The @InjectMocks creates an instance of the class and injects the mocks that were created with the @Mock annotation into this instance.
PAVAN VADAPALLI
Popular
Talk to our experts. We’re available 24/7.
Indian Nationals
1800 210 2020
Foreign Nationals
+918045604032
upGrad does not grant credit; credits are granted, accepted or transferred at the sole discretion of the relevant educational institution offering the diploma or degree. We advise you to enquire further regarding the suitability of this program for your academic, professional requirements and job prospects before enrolling. upGrad does not make any representations regarding the recognition or equivalence of the credits or credentials awarded, unless otherwise expressly stated. Success depends on individual qualifications, experience, and efforts in seeking employment.
upGrad does not grant credit; credits are granted, accepted or transferred at the sole discretion of the relevant educational institution offering the diploma or degree. We advise you to enquire further regarding the suitability of this program for your academic, professional requirements and job prospects before enr...