The verify
method in Mockito is used to confirm that certain interactions with mock objects happened. This is crucial for ensuring that the methods you expect to be called on your mocks are actually called, and with the correct parameters. Mockito provides two overloaded versions of the verify
method to handle different verification scenarios.
Table of Contents
- Introduction
verify
Method Syntax- Examples
- Basic Usage
- Verifying with a Verification Mode
- Real-World Use Case
- Conclusion
Introduction
Mockito is a popular library in Java for creating and managing mock objects. The verify
method allows you to check that specific methods were called on your mock objects. This helps ensure that your tests accurately reflect the expected interactions with your dependencies.
verify Method Syntax
Basic Verification
static <T> T verify(T mock)
Verifies that a certain behavior happened once.
Verification with Mode
static <T> T verify(T mock, VerificationMode mode)
Verifies that a certain behavior happened at least once, a specific number of times, or never, depending on the VerificationMode
.
Examples
Basic Usage
Verify that a method was called once on the mock object.
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import org.junit.jupiter.api.Test;
public class BasicVerifyTest {
@Test
void testVerify() {
UserService mockUserService = mock(UserService.class);
mockUserService.getUserDetails("123");
// Verify that getUserDetails was called once with argument "123"
verify(mockUserService).getUserDetails("123");
}
}
class UserService {
public String getUserDetails(String userId) {
return "Real user details for " + userId;
}
}
Verifying with a Verification Mode
Verify that a method was called a specific number of times on the mock object.
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.times;
import org.junit.jupiter.api.Test;
public class VerifyWithModeTest {
@Test
void testVerifyWithMode() {
UserService mockUserService = mock(UserService.class);
mockUserService.getUserDetails("123");
mockUserService.getUserDetails("123");
// Verify that getUserDetails was called exactly twice with argument "123"
verify(mockUserService, times(2)).getUserDetails("123");
}
}
class UserService {
public String getUserDetails(String userId) {
return "Real user details for " + userId;
}
}
Real-World Use Case
Verifying Interactions in a Service with Dependency
In a real-world scenario, you might have a UserService
class that depends on a UserRepository
. Using Mockito, you can verify that the UserRepository
methods are called the expected number of times.
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.times;
import org.junit.jupiter.api.Test;
class User {
private String id;
private String name;
public User(String id, String name) {
this.id = id;
this.name = name;
}
public String getName() {
return name;
}
}
interface UserRepository {
User findUserById(String id);
void saveUser(User user);
}
class UserService {
private final UserRepository userRepository;
public UserService(UserRepository userRepository) {
this.userRepository = userRepository;
}
public String getUserName(String userId) {
User user = userRepository.findUserById(userId);
return user != null ? user.getName() : null;
}
public void createUser(User user) {
userRepository.saveUser(user);
}
}
public class UserServiceTest {
@Test
void testVerifyInteractions() {
UserRepository mockUserRepository = mock(UserRepository.class);
UserService userService = new UserService(mockUserRepository);
userService.getUserName("123");
userService.createUser(new User("456", "Jane Doe"));
// Verify that findUserById was called once with argument "123"
verify(mockUserRepository).findUserById("123");
// Verify that saveUser was called once with a User object
verify(mockUserRepository).saveUser(new User("456", "Jane Doe"));
}
@Test
void testVerifyWithTimes() {
UserRepository mockUserRepository = mock(UserRepository.class);
UserService userService = new UserService(mockUserRepository);
userService.getUserName("123");
userService.getUserName("123");
// Verify that findUserById was called exactly twice with argument "123"
verify(mockUserRepository, times(2)).findUserById("123");
}
}
In this example, the UserServiceTest
class uses Mockito’s verify
method to confirm that the UserRepository
methods are called the expected number of times. The tests check both single and multiple invocations.
Conclusion
The verify
method in Mockito is a crucial tool for confirming interactions with mock objects. By using verify
, you can ensure that your tests accurately reflect the expected behavior and interactions with your dependencies. This helps improve the reliability and accuracy of your tests.