Mocking Feign Client in Spring Boot Microservices

When building microservices with Spring Boot, Feign clients are a popular choice for making HTTP requests to other services. In testing, mocking these clients is crucial to isolate your service and ensure that your unit tests are not dependent on external services. Let’s take a closer look at how to mock a Feign client, using an e-commerce project as an example.

Understanding the Need for Mocking Feign Clients 

In a microservices architecture, services often need to communicate with each other, and Feign clients facilitate this communication in Spring Boot applications. However, during testing, you don't want these calls to reach the actual services. This is where mocking comes into play. By mocking a Feign client, you can simulate its behavior and focus on testing your service’s business logic.

E-commerce Example: Setting Up the Scenario 

Imagine we are working on an e-commerce application. Our application has a service called ProductService that checks the availability of a product by communicating with an external InventoryService using a Feign client. 

Feign Client Interface for Inventory Service 

First, we define a Feign client interface to interact with the inventory service:

@FeignClient(name = "inventory-service")
public interface InventoryClient {
    @GetMapping("/inventory/{productId}")
    InventoryResponse checkProductInventory(@PathVariable("productId") String productId);
}

public class InventoryResponse {
    private String productId;
    private int availableQuantity;
    // Constructors, Getters, and Setters
}

ProductService Using the Feign Client 

The ProductService uses the InventoryClient to check product availability:
@Service
public class ProductService {

    private final InventoryClient inventoryClient;

    @Autowired
    public ProductService(InventoryClient inventoryClient) {
        this.inventoryClient = inventoryClient;
    }

    public boolean isProductInStock(String productId) {
        InventoryResponse response = inventoryClient.checkProductInventory(productId);
        return response.getAvailableQuantity() > 0;
    }
}

Writing Tests by Mocking the Feign Client

To test our ProductService, we need to mock the InventoryClient using Mockito.

Let's include the necessary testing dependencies like Mockito and Spring Boot Starter Test in your project. Then, create a test class ProductServiceTest, and add the following code to it:

@SpringBootTest
public class ProductServiceTest {

    @MockBean
    private InventoryClient mockInventoryClient;

    @Autowired
    private ProductService productService;

    @Test
    public void whenProductInStock_thenProductServiceReturnsTrue() {
        // Arrange
        String testProductId = "12345";
        InventoryResponse mockResponse = new InventoryResponse(testProductId, 10);
        Mockito.when(mockInventoryClient.checkProductInventory(testProductId)).thenReturn(mockResponse);

        // Act
        boolean isInStock = productService.isProductInStock(testProductId);

        // Assert
        assertTrue(isInStock, "Product should be in stock");
    }
}
In this test: 
  • We use @MockBean to create a mock of InventoryClient
  • We define what the mock should return using Mockito’s when method
  • We then test the ProductService to verify it behaves correctly using the mocked Feign client.

Why Mock Feign Clients? 

Mocking Feign clients in tests ensures: 

Isolation: Tests remain independent of external services. 

Reliability: Tests aren't affected by the availability of external services. 

Speed: Tests run faster as they don't make actual network calls.

Conclusion 

Mocking Feign clients in a Spring Boot microservices application is essential for writing effective and isolated unit tests. It allows you to simulate the behavior of external service calls, ensuring that your tests focus solely on the business logic of your service. This approach is crucial for developing robust and reliable microservices in Spring Boot.

Related Microservices Tutorials/Guides using Spring Boot and Spring Cloud

✅ What are Microservices and How to Build Microservices in Java?

✅ Spring Boot Microservices Architecture

✅ What is Spring Cloud?

✅ Spring Boot Microservices Communication Example using RestTemplate

✅ Spring Boot Microservices Communication Example using WebClient

✅ Spring Boot Microservices Communication Example using Spring Cloud Open Feign

✅ Spring Boot Microservices - Spring Cloud Config Server

✅ Spring Boot Microservices - Spring Cloud Netflix Eureka-based Service Registry

✅ Spring Boot Microservices - Spring Cloud API Gateway

✅ Event-Driven Microservices using Spring Boot and Kafka

✅ Microservices Project using Spring Boot



Comments