The only
method in the Mockito framework is used to verify that a method on a mock object was the only method that was invoked. This means that no other methods should have been called on the mock object. This is useful when you want to ensure that a specific interaction was the sole interaction with a mock.
Table of Contents
- Introduction
only
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 only
method, which belongs to the Mockito
class, allows you to verify that a particular method was the only one invoked on a mock object. This can help ensure that your code interacts with the mock in a very specific way without invoking any other methods.
only Method Syntax
Verifying Only One Method Invocation
import static org.mockito.Mockito.only;
static VerificationMode only()
Specifies that the given method should be the only one invoked.
Returns:
- A
VerificationMode
object that can be used to verify the method calls.
Examples
Basic Usage
Verify that a method was the only one called on a single mock object.
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.only;
import org.junit.jupiter.api.Test;
public class BasicOnlyTest {
@Test
void testOnly() {
UserService mockUserService = mock(UserService.class);
// Perform the interaction
mockUserService.deleteUser("123");
// Verify that deleteUser was the only method called
verify(mockUserService, only()).deleteUser("123");
}
}
class UserService {
public void deleteUser(String userId) {
// Method implementation
}
public void updateUser(String userId) {
// Method implementation
}
}
Verifying Multiple Mocks
Verify that methods were the only ones called on multiple mock objects.
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.only;
import org.junit.jupiter.api.Test;
public class MultipleOnlyTest {
@Test
void testOnlyMultipleMocks() {
UserService mockUserService = mock(UserService.class);
NotificationService mockNotificationService = mock(NotificationService.class);
// Perform the interactions
mockUserService.deleteUser("123");
mockNotificationService.sendNotification("User 123 deleted");
// Verify that deleteUser was the only method called on UserService
verify(mockUserService, only()).deleteUser("123");
// Verify that sendNotification was the only method called on NotificationService
verify(mockNotificationService, only()).sendNotification("User 123 deleted");
}
}
class UserService {
public void deleteUser(String userId) {
// Method implementation
}
public void updateUser(String userId) {
// Method implementation
}
}
interface NotificationService {
void sendNotification(String message);
}
Real-World Use Case
Ensuring Specific Method Interactions
In a real-world scenario, you might want to ensure that certain methods in your service are the only ones called under specific conditions. This can help validate that your code does not perform any unintended operations.
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.only;
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 cancelOrder(String orderId) {
notificationService.sendNotification("Order " + orderId + " canceled");
}
}
public class OrderServiceTest {
@Test
void testOrderServiceInteractionsOnly() {
PaymentService mockPaymentService = mock(PaymentService.class);
NotificationService mockNotificationService = mock(NotificationService.class);
OrderService orderService = new OrderService(mockPaymentService, mockNotificationService);
// Call the cancelOrder method
orderService.cancelOrder("123");
// Verify that sendNotification was the only method called on NotificationService
verify(mockNotificationService, only()).sendNotification("Order 123 canceled");
// Verify that no method was called on PaymentService
verify(mockPaymentService, never()).processPayment("123");
}
}
In this example, the OrderServiceTest
class uses Mockito’s only
method to ensure that sendNotification
is the only method called on the NotificationService
mock when an order is canceled. Additionally, it verifies that no methods are called on the PaymentService
mock. This helps ensure that the OrderService
methods perform the correct operations without any unintended actions.
Conclusion
The only
verification mode in Mockito is used for ensuring that a specific method is the only one invoked on a mock object. By using only
, you can validate that your code interacts with the mock in a very specific way, improving the accuracy and reliability of your tests. This helps ensure that your code behaves as expected and avoids unintended interactions.