Testing microservices based applications can be challenging due to the complexity of their architectures and diverse testing needs. This post aims to clarify the essence of microservices testing with a simple example.Following Mike Cohn's testing pyramid, we prioritise smaller tests at the base (unit, integration) to catch issues early. Fixing problems quickly and easily at this level saves time and resources compared to troubleshooting complex end-to-end scenarios later.

Understanding Microservices Architecture

Microservices architecture is an approach to building applications as a collection of small, independent services. Each service owns a specific business capability and communicates with others through well-defined APIs. This modular design offers several advantages, including improved scalability, maintainability, and fault isolation. Here are the key components involved:

  • BFF (Backend for Frontend): Acts as a facade for the frontend, handling user interactions and data formatting for specific needs.

  • Business Service: Encapsulates core functionalities and business logic, independent of presentation layers.

  • Adapter Service: Connects the business service to databases or other persistent storage, handling data access and formatting.

  • External Service: Provides additional functionalities not directly available within the application, integrating through APIs.

Testing a Sample feature: Submitting a Product Review

Let's consider the scenario of a user submitting a product review on an e-commerce store. We'll explore how different layers of the architecture contribute to this functionality:

Microservices Interaction:

  • Front-end: Presents a form for user input.
  • BFF: Receives user review data, validates it, and sends it to the business service.
  • Business Service: Processes the review, performs business logic, and sends it to the adapter service.
  • Adapter Service: Interfaces with the database to persist the review.
  • External Service: Integrates with external systems for reputation scoring or sentiment analysis.

Building Confidence Through Layered Testing

By strategically applying different test types, we can systematically build confidence in the application:

Unit Tests:

  • Test if the review form accepts only valid characters and lengths for name, rating, and review text.

  • Test if the review content is checked for correctness.

  • Test if the review data is successfully stored in the database with correct fields and values.

Integration Tests:

  • BFF to  Business Service: Simulate user submitting a review, verify data transfer, and response handling.

  • Business Service to Adapter Service: Test the data exchange and persistence operations.

  • Business Service to  External Service: Simulate external API calls for sentiment analysis and verify response integration.

Contract Tests:

  • Define the expected format for review data sent from the BFF to the business service.

  • Verify that the business service responds with valid data adhering to the agreed-upon contract.

System Tests:

  • Deploy the BFF and Business Service in a simulated environment with mocked dependencies.

  • Test complete review submission flow, including user authentication, data validation, and response generation.

End-to-End Tests:

  • Simulate a real user journey through the entire application, including external service interactions.

  • Test review submission, data persistence, sentiment analysis display and overall user experience.

By leveraging different testing types in a layered approach, we can systematically build confidence in the functionality and reliability of microservices applications. This ensures a seamless user experience and simplifies the development and maintenance process.