🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
Introduction
The doNothing() method in Mockito is used to specify that a method call on a mock object should do nothing. This is particularly useful for void methods, where you want to avoid the actual execution of the method without throwing an exception. In this chapter, we will explore how to use doNothing() in Mockito with examples and discuss its applications.
Syntax
The doNothing() method is used in conjunction with the doAnswer(), doThrow(), doCallRealMethod(), etc. methods in Mockito's do*() family. The general syntax is as follows:
doNothing().when(mockObject).voidMethod();
Key Points:
doNothing()is used withwhento define the behavior of void methods.- It is particularly useful for methods that do not return a value and do not need to be verified for their return values.
Example
Consider a scenario where we have a UserService class with a sendNotification method that sends notifications to users. We want to test another method in the UserService class without actually sending notifications. Here, doNothing() can be useful.
Example Code
First, let's define the UserService class and a NotificationService dependency:
public class NotificationService {
public void sendNotification(String message) {
// Code to send notification
}
}
public class UserService {
private final NotificationService notificationService;
public UserService(NotificationService notificationService) {
this.notificationService = notificationService;
}
public void registerUser(String user) {
// User registration logic
notificationService.sendNotification("User registered: " + user);
}
}
Now, let's create a test for the UserService class, using doNothing() to mock the sendNotification method:
import static org.mockito.Mockito.*;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.junit.jupiter.api.extension.ExtendWith;
@ExtendWith(MockitoExtension.class)
public class UserServiceTest {
@Mock
private NotificationService notificationService;
@InjectMocks
private UserService userService;
@Test
public void testRegisterUser() {
// Arrange
doNothing().when(notificationService).sendNotification(anyString());
// Act
userService.registerUser("testUser");
// Assert
verify(notificationService).sendNotification("User registered: testUser");
}
}
Key Points:
doNothing()ensures that thesendNotificationmethod does not execute its actual implementation.verifyis used to ensure that thesendNotificationmethod was called with the correct argument.
Applications
- Testing Methods with Void Return Types:
doNothing()is ideal for methods that do not return a value but still need to be mocked to prevent their actual execution. - Avoiding Side Effects: When you want to test a method without triggering side effects (e.g., sending emails, writing to a database),
doNothing()can be used to mock those method calls. - Partial Mocks: In cases where only certain methods of a class need to be mocked while allowing others to execute normally,
doNothing()can be combined with otherdo*()methods.
Conclusion
The doNothing() method in Mockito is used to handle void methods in unit tests. It allows you to define behavior for methods that do not return a value, preventing their actual execution and avoiding side effects. By understanding how to use doNothing() effectively, you can create more focused and reliable unit tests for your Java applications.
Related Mockito Methods
Mockito mock()Mockito spy()
Mockito when()
Mockito thenThrow()
Mockito verify()
Mockito times()
Mockito never()
Mockito any()
Mockito eq()
Mockito inOrder()
Mockito doReturn()
Mockito doThrow()
Mockito doAnswer()
Mockito timeout()
Mockito ArgumentMatchers
Comments
Post a Comment
Leave Comment