The eq method in the Mockito framework is part of the ArgumentMatchers class. It is used to match an argument that is equal to the given value. This is particularly useful when you need to verify or stub method calls where the specific argument should be equal to a certain value.
Table of Contents
- Introduction
eqMethod Syntax- Examples
- Matching Primitive Types
- Matching Object Types
- Real-World Use Case
- Conclusion
Introduction
Mockito is a popular library in Java for creating and managing mock objects. The eq method, which belongs to the ArgumentMatchers class, allows you to specify that an argument should be equal to a given value. This can be useful for simplifying tests where the specific value of an argument is important.
eq Method Syntax
Matching Primitive Types
Matching a boolean Value
import org.mockito.ArgumentMatchers;
static boolean eq(boolean value)
Matches a boolean argument that is equal to the given value.
Matching a byte Value
import org.mockito.ArgumentMatchers;
static byte eq(byte value)
Matches a byte argument that is equal to the given value.
Matching a char Value
import org.mockito.ArgumentMatchers;
static char eq(char value)
Matches a char argument that is equal to the given value.
Matching a double Value
import org.mockito.ArgumentMatchers;
static double eq(double value)
Matches a double argument that is equal to the given value.
Matching a float Value
import org.mockito.ArgumentMatchers;
static float eq(float value)
Matches a float argument that is equal to the given value.
Matching an int Value
import org.mockito.ArgumentMatchers;
static int eq(int value)
Matches an int argument that is equal to the given value.
Matching a long Value
import org.mockito.ArgumentMatchers;
static long eq(long value)
Matches a long argument that is equal to the given value.
Matching a short Value
import org.mockito.ArgumentMatchers;
static short eq(short value)
Matches a short argument that is equal to the given value.
Matching Object Types
Matching an Object Value
import org.mockito.ArgumentMatchers;
static <T> T eq(T value)
Matches an Object argument that is equal to the given value.
Examples
Matching Primitive Types
Matching an int Argument
Use eq to match an int 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 MatchingIntArgumentTest {
@Test
void testEqInt() {
MathService mockMathService = mock(MathService.class);
// Stub the method to return a specific value
when(mockMathService.add(ArgumentMatchers.eq(5), ArgumentMatchers.eq(10))).thenReturn(15);
// Call the method with matching arguments
int result = mockMathService.add(5, 10);
// Verify the result
assertEquals(15, result);
}
}
interface MathService {
int add(int a, int b);
}
Matching a boolean Argument
Use eq to match a boolean 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 MatchingBooleanArgumentTest {
@Test
void testEqBoolean() {
UserService mockUserService = mock(UserService.class);
// Stub the method to return a specific value
when(mockUserService.isActive(ArgumentMatchers.eq(true))).thenReturn("User is active");
// Call the method with matching arguments
String result = mockUserService.isActive(true);
// Verify the result
assertEquals("User is active", result);
}
}
class UserService {
public String isActive(boolean active) {
return active ? "User is active" : "User is inactive";
}
}
Matching Object Types
Matching a String Argument
Use eq to match a String 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 MatchingStringArgumentTest {
@Test
void testEqString() {
NotificationService mockNotificationService = mock(NotificationService.class);
// Stub the method to return a specific value
when(mockNotificationService.sendNotification(ArgumentMatchers.eq("Welcome"), ArgumentMatchers.eq("user@example.com"))).thenReturn("Notification sent");
// Call the method with matching arguments
String result = mockNotificationService.sendNotification("Welcome", "user@example.com");
// Verify the result
assertEquals("Notification sent", result);
}
}
interface NotificationService {
String sendNotification(String subject, String recipientEmail);
}
Real-World Use Case
Simplifying Tests for Services with Specific Argument Values
In a real-world scenario, you might need to test services with methods that take specific argument values. Using eq can simplify these tests by allowing you to focus on the behavior you are testing rather than the specific content of the arguments.
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.eq("user123"), ArgumentMatchers.eq("Notification"), ArgumentMatchers.eq("Your account has been updated."));
}
}
In this example, the UserServiceTest class uses Mockito’s eq method to match specific argument values for the sendEmail method. This simplifies the test by allowing you to verify the interaction based on the exact values of the arguments.
Conclusion
The eq method in Mockito is used for matching arguments that are equal to a given value in mocked method calls. By using eq, 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.