Introduction
In this chapter, we will learn about verifying no interactions using Mockito. Verifying that no interactions have occurred with a mock object is important for ensuring that certain methods are not called during the execution of your code. This helps you confirm that your code is working correctly and adheres to the intended logic.
What is Verifying No Interactions?
Verifying no interactions involves checking that no methods were called on a mock object during the test. This can be useful for ensuring that certain dependencies are not used or that specific methods are not called under certain conditions.
Example: OrderService Class and OrderServiceTest Class
Class Under Test: OrderService
public class OrderService {
private PaymentService paymentService;
private InventoryService inventoryService;
public OrderService(PaymentService paymentService, InventoryService inventoryService) {
this.paymentService = paymentService;
this.inventoryService = inventoryService;
}
public void placeOrder(Order order) {
if (inventoryService.isInStock(order.getProductId())) {
paymentService.processPayment(order.getAmount());
inventoryService.updateStock(order.getProductId());
}
}
public void cancelOrder(Order order) {
if (!inventoryService.isInStock(order.getProductId())) {
paymentService.refundPayment(order.getAmount());
}
}
}
Supporting Classes: PaymentService and InventoryService
public class PaymentService {
public void processPayment(double amount) {
// Process the payment
}
public void refundPayment(double amount) {
// Refund the payment
}
}
public class InventoryService {
public boolean isInStock(String productId) {
// Check if the product is in stock
return true;
}
public void updateStock(String productId) {
// Update the stock for the product
}
}
Order Class
public class Order {
private String productId;
private double amount;
public Order(String productId, double amount) {
this.productId = productId;
this.amount = amount;
}
public String getProductId() {
return productId;
}
public double getAmount() {
return amount;
}
}
Test Class: OrderServiceTest
import static org.mockito.Mockito.*;
import org.junit.jupiter.api.Test;
public class OrderServiceTest {
@Test
void testCancelOrder_VerifyNoInteractions() {
// Create mock objects for PaymentService and InventoryService
PaymentService paymentService = mock(PaymentService.class);
InventoryService inventoryService = mock(InventoryService.class);
// Stub the isInStock method to return true
when(inventoryService.isInStock(anyString())).thenReturn(true);
// Create an instance of OrderService with the mock dependencies
OrderService orderService = new OrderService(paymentService, inventoryService);
// Create an order
Order order = new Order("product123", 100.0);
// Cancel the order
orderService.cancelOrder(order);
// Verify that no interactions occurred with PaymentService
verifyNoInteractions(paymentService);
}
@Test
void testPlaceOrder_VerifyNoMoreInteractions() {
// Create mock objects for PaymentService and InventoryService
PaymentService paymentService = mock(PaymentService.class);
InventoryService inventoryService = mock(InventoryService.class);
// Stub the isInStock method to return true
when(inventoryService.isInStock(anyString())).thenReturn(true);
// Create an instance of OrderService with the mock dependencies
OrderService orderService = new OrderService(paymentService, inventoryService);
// Create an order
Order order = new Order("product123", 100.0);
// Place the order
orderService.placeOrder(order);
// Verify interactions
verify(inventoryService).isInStock("product123");
verify(paymentService).processPayment(100.0);
verify(inventoryService).updateStock("product123");
// Verify that no more interactions occurred with the mocks
verifyNoMoreInteractions(paymentService, inventoryService);
}
}
Explanation
-
Creating Mocks:
PaymentService paymentService = mock(PaymentService.class); InventoryService inventoryService = mock(InventoryService.class);
These lines create mock objects of the
PaymentService
andInventoryService
classes. -
Stubbing Method:
when(inventoryService.isInStock(anyString())).thenReturn(true);
This line stubs the
isInStock
method of theInventoryService
mock to returntrue
when called with any string. -
Creating an Instance of OrderService:
OrderService orderService = new OrderService(paymentService, inventoryService);
This line creates an instance of the
OrderService
class with the mock dependencies. -
Creating an Order:
Order order = new Order("product123", 100.0);
This line creates an
Order
object with a product ID and an amount. -
Canceling the Order:
orderService.cancelOrder(order);
This line calls the
cancelOrder
method on theOrderService
instance with the createdOrder
object. -
Verifying No Interactions:
verifyNoInteractions(paymentService);
This line verifies that no interactions occurred with the
PaymentService
mock. Since the product is in stock, therefundPayment
method should not be called. -
Placing the Order:
orderService.placeOrder(order);
This line calls the
placeOrder
method on theOrderService
instance with the createdOrder
object. -
Verifying Interactions:
verify(inventoryService).isInStock("product123"); verify(paymentService).processPayment(100.0); verify(inventoryService).updateStock("product123");
These lines verify that the
isInStock
,processPayment
, andupdateStock
methods were called with the correct arguments. -
Verifying No More Interactions:
verifyNoMoreInteractions(paymentService, inventoryService);
This line verifies that no further interactions occurred with the
paymentService
andinventoryService
mocks beyond what has already been verified.
Conclusion
Verifying no interactions using Mockito ensures that certain methods are not called during the execution of your code. By using methods like verifyNoInteractions()
and verifyNoMoreInteractions()
, you can confirm that your code is functioning correctly and adhering to the intended logic. This is particularly useful for ensuring that specific dependencies are not used or that certain methods are not called under certain conditions. Verifying no interactions is a crucial part of writing effective and reliable unit tests.