The notNull
method in the Mockito framework is part of the ArgumentMatchers
class. It is used to match any argument that is not null. This is particularly useful when you need to verify or stub method calls where the argument should not be null.
Table of Contents
- Introduction
notNull
Method Syntax- Examples
- Basic Usage
- Using
notNull
with Specific Types
- Real-World Use Case
- Conclusion
Introduction
Mockito is a popular library in Java for creating and managing mock objects. The notNull
method, which belongs to the ArgumentMatchers
class, allows you to specify that an argument should not be null. This can be useful for simplifying tests where the specific value of an argument is not important, but it must not be null.
notNull Method Syntax
Matching Any Non-Null Argument
import org.mockito.ArgumentMatchers;
static <T> T notNull()
Matches any non-null argument.
Matching a Non-Null Argument of a Specific Type
import org.mockito.ArgumentMatchers;
static <T> T notNull(Class<T> type)
Matches any non-null argument of the specified type.
Parameters:
type
: The class of the type to be matched.
Returns:
- An argument of type
T
that is not null.
Examples
Basic Usage
Use notNull
to match any non-null argument in a mocked method call.
import static org.mockito.Mockito.*;
import org.mockito.ArgumentMatchers;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
public class BasicNotNullTest {
@Test
void testNotNull() {
UserService mockUserService = mock(UserService.class);
// Stub the method to return a specific value
when(mockUserService.getUserDetails(ArgumentMatchers.notNull())).thenReturn("User details");
// Call the method with a non-null argument
String result = mockUserService.getUserDetails("user123");
// Verify the result
assertEquals("User details", result);
}
}
class UserService {
public String getUserDetails(String userId) {
return "Real user details for " + userId;
}
}
Using notNull with Specific Types
Use notNull
to match a non-null argument of a specific type in a mocked method call.
import static org.mockito.Mockito.*;
import org.mockito.ArgumentMatchers;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
public class SpecificTypeNotNullTest {
@Test
void testNotNullWithSpecificType() {
UserService mockUserService = mock(UserService.class);
// Stub the method to return a specific value
when(mockUserService.getUserDetails(ArgumentMatchers.notNull(String.class))).thenReturn("User details");
// Call the method with a non-null argument
String result = mockUserService.getUserDetails("user123");
// Verify the result
assertEquals("User details", result);
}
}
class UserService {
public String getUserDetails(String userId) {
return "Real user details for " + userId;
}
}
Real-World Use Case
Simplifying Tests for Services with Non-Null Argument Requirements
In a real-world scenario, you might need to test services with methods that require non-null arguments. Using notNull
can simplify these tests by allowing you to focus on the behavior you are testing rather than the specific content of the arguments, as long as they are not null.
import static org.mockito.Mockito.*;
import org.mockito.ArgumentMatchers;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
interface EmailService {
void sendEmail(String recipient, String subject, String body);
}
class UserService {
private final EmailService emailService;
public UserService(EmailService emailService) {
this.emailService = emailService;
}
public void notifyUser(String userId, String message) {
// User notification logic
emailService.sendEmail(userId, "Notification", message);
}
}
public class UserServiceTest {
@Test
void testNotifyUser() {
EmailService mockEmailService = mock(EmailService.class);
UserService userService = new UserService(mockEmailService);
// Call the method
userService.notifyUser("user123", "Your account has been updated.");
// Verify the interaction
verify(mockEmailService).sendEmail(ArgumentMatchers.notNull(), ArgumentMatchers.eq("Notification"), ArgumentMatchers.notNull());
}
}
In this example, the UserServiceTest
class uses Mockito’s notNull
method to match any non-null arguments for the sendEmail
method. This simplifies the test by allowing you to verify the interaction based on the non-null nature of the arguments, without worrying about their exact values.
Conclusion
The notNull
method in Mockito is used for matching any non-null argument in mocked method calls. By using notNull
, you can simplify your tests and focus on the behavior you are testing, rather than the exact content of the arguments. This helps ensure that your tests are flexible, comprehensive, and easy to maintain.