The calls
method in the Mockito framework is used to verify that a method on a mock object was called a specific number of times. This method allows for non-greedy verification in order, meaning that it will check for the exact number of invocations without allowing for additional calls beyond what is specified.
Table of Contents
- Introduction
calls
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 calls
method, which belongs to the Mockito
class, allows you to specify that a method should be called exactly the given number of times on a mock object. This can help ensure that your code performs the expected number of operations.
calls Method Syntax
Verifying a Specific Number of Invocations
import static org.mockito.Mockito.calls;
static VerificationMode calls(int wantedNumberOfInvocations)
Specifies that a method should have been called exactly the given number of times.
Parameters:
wantedNumberOfInvocations
: The exact 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 exactly 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.calls;
import org.junit.jupiter.api.Test;
public class BasicCallsTest {
@Test
void testCalls() {
UserService mockUserService = mock(UserService.class);
// Perform some interactions
mockUserService.deleteUser("123");
mockUserService.deleteUser("123");
// Verify that deleteUser was called exactly twice
verify(mockUserService, calls(2)).deleteUser("123");
}
}
class UserService {
public void deleteUser(String userId) {
// Method implementation
}
}
Verifying Multiple Mocks
Verify that methods were called exactly the 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.calls;
import org.junit.jupiter.api.Test;
public class MultipleCallsTest {
@Test
void testCallsMultipleMocks() {
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 exactly twice
verify(mockUserService, calls(2)).deleteUser("123");
// Verify that sendNotification was called exactly twice
verify(mockNotificationService, calls(2)).sendNotification("User 123 deleted");
}
}
class UserService {
public void deleteUser(String userId) {
// Method implementation
}
}
interface NotificationService {
void sendNotification(String message);
}
Real-World Use Case
Ensuring Exact Method Calls in a Service
In a real-world scenario, you might want to ensure that certain methods in your service are called exactly a specified number of times. This can help in validating that your code performs the correct number of operations.
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.calls;
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 testOrderServiceInteractionsCalls() {
PaymentService mockPaymentService = mock(PaymentService.class);
NotificationService mockNotificationService = mock(NotificationService.class);
OrderService orderService = new OrderService(mockPaymentService, mockNotificationService);
// Call methods on OrderService
orderService.processMultiplePayments("123", 3);
// Verify that processPayment was called exactly three times
verify(mockPaymentService, calls(3)).processPayment("123");
// Call another method on OrderService
orderService.placeOrder("456");
// Verify that processPayment was called exactly once
verify(mockPaymentService, calls(1)).processPayment("456");
// Verify that sendNotification was called exactly once
verify(mockNotificationService, calls(1)).sendNotification("Order 456 placed");
}
}
In this example, the OrderServiceTest
class uses Mockito’s calls
method to ensure that processPayment
is called exactly three times and sendNotification
is called exactly once. This helps ensure that the OrderService
methods perform the correct number of operations.
Conclusion
The calls
verification mode in Mockito is used for ensuring that methods on mock objects are called exactly the specified number of times. By using calls
, you can validate that your code performs the correct number of operations, improving the accuracy and reliability of your tests. This helps ensure that your code behaves as expected and performs optimally.