The atLeast
method in Mockito is used to verify that a method on a mock object was called at least a specified number of times. This is useful when you want to ensure that a method is invoked a minimum number of times, but you do not care if it was called more than that.
Table of Contents
- Introduction
atLeast
Method Syntax- Examples
- Basic Usage
- Verifying Multiple Mocks
- Real-World Use Case
- Conclusion
Introduction
Mockito is a popular library in Java for creating and managing mock objects. The atLeast
verification mode allows you to specify that a method should have been called at least a certain number of times on a mock object. This can help ensure that important methods are invoked as expected during the execution of your code.
atLeast Method Syntax
Verifying At Least a Minimum Number of Invocations
import static org.mockito.Mockito.atLeast;
static VerificationMode atLeast(int minNumberOfInvocations)
Specifies that a method should have been called at least the given number of times.
Parameters:
minNumberOfInvocations
: The minimum number of times the method should have been called.
Returns:
- A
VerificationMode
object that can be used to verify the method calls.
Examples
Basic Usage
Verify that a method was called at least a specified number of times on a single mock object.
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.atLeast;
import org.junit.jupiter.api.Test;
public class BasicAtLeastTest {
@Test
void testAtLeast() {
UserService mockUserService = mock(UserService.class);
// Perform some interactions
mockUserService.deleteUser("123");
mockUserService.deleteUser("123");
mockUserService.deleteUser("123");
// Verify that deleteUser was called at least twice
verify(mockUserService, atLeast(2)).deleteUser("123");
}
}
class UserService {
public void deleteUser(String userId) {
// Method implementation
}
}
Verifying Multiple Mocks
Verify that methods were called at least a specified number of times on multiple mock objects.
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.atLeast;
import org.junit.jupiter.api.Test;
public class MultipleAtLeastTest {
@Test
void testAtLeastMultipleMocks() {
UserService mockUserService = mock(UserService.class);
NotificationService mockNotificationService = mock(NotificationService.class);
// Perform some interactions
mockUserService.deleteUser("123");
mockUserService.deleteUser("123");
mockNotificationService.sendNotification("User 123 deleted");
mockNotificationService.sendNotification("User 123 deleted");
// Verify that deleteUser was called at least twice
verify(mockUserService, atLeast(2)).deleteUser("123");
// Verify that sendNotification was called at least once
verify(mockNotificationService, atLeast(1)).sendNotification("User 123 deleted");
}
}
class UserService {
public void deleteUser(String userId) {
// Method implementation
}
}
interface NotificationService {
void sendNotification(String message);
}
Real-World Use Case
Ensuring Minimum Method Calls in a Service
In a real-world scenario, you might want to ensure that certain critical methods in your service are called at least a specified number of times. This can be crucial for validating that essential operations are performed as expected.
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.atLeast;
import org.junit.jupiter.api.Test;
interface PaymentService {
void processPayment(String orderId);
}
interface NotificationService {
void sendNotification(String message);
}
class OrderService {
private final PaymentService paymentService;
private final NotificationService notificationService;
public OrderService(PaymentService paymentService, NotificationService notificationService) {
this.paymentService = paymentService;
this.notificationService = notificationService;
}
public void placeOrder(String orderId) {
paymentService.processPayment(orderId);
notificationService.sendNotification("Order " + orderId + " placed");
}
public void processMultiplePayments(String orderId, int times) {
for (int i = 0; i < times; i++) {
paymentService.processPayment(orderId);
}
}
}
public class OrderServiceTest {
@Test
void testOrderServiceInteractionsAtLeast() {
PaymentService mockPaymentService = mock(PaymentService.class);
NotificationService mockNotificationService = mock(NotificationService.class);
OrderService orderService = new OrderService(mockPaymentService, mockNotificationService);
// Call methods on OrderService
orderService.placeOrder("123");
orderService.processMultiplePayments("123", 3);
// Verify that processPayment was called at least three times
verify(mockPaymentService, atLeast(3)).processPayment("123");
// Verify that sendNotification was called at least once
verify(mockNotificationService, atLeast(1)).sendNotification("Order 123 placed");
}
}
In this example, the OrderServiceTest
class uses Mockito’s atLeast
method to ensure that processPayment
is called at least three times and sendNotification
is called at least once. This helps ensure that critical operations in the OrderService
are performed as expected.
Conclusion
The atLeast
verification mode in Mockito is used for ensuring that methods on mock objects are called at least a specified number of times. By using atLeast
, you can validate that important methods are invoked the required number of times, improving the accuracy and reliability of your tests. This helps ensure that your code behaves as expected and that essential operations are performed.