How do you mock microservices during testing?

Mocking microservices during testing allows us to verify the behavior of a service without depending on actual external services. Instead of calling real microservices, we simulate their responses using mock servers or mock objects. This makes tests faster, more reliable, and independent of network or service availability.

Key Points: • Mocking removes dependencies on external services during testing. • It helps create predictable test scenarios by returning predefined responses. • Common tools include WireMock, Mockito, MockWebServer, and Spring Cloud Contract.

Example: Suppose an Order Service calls a Payment Service to process payments.

In production:

Order Service ↓ Payment Service

During testing:

Order Service ↓ WireMock Server ↓ Mock Response

The Order Service believes it is communicating with the real Payment Service, while WireMock returns predefined responses.

Code Example:

WireMock Setup:

stubFor(post(urlEqualTo("/payments"))

    .willReturn(
        aResponse()

.withStatus(200) .withBody( "{\"status\":\"SUCCESS\"}" )));

When the application calls:

POST /payments

WireMock returns:

{ "status": "SUCCESS" }

Common Approaches:

1. WireMock

• Simulates REST APIs. • Supports request matching. • Returns predefined responses. • Ideal for integration testing.

2. Mockito

• Mocks service-layer dependencies. • Commonly used in unit testing.

Example:

@Mock
private PaymentService paymentService;

when(paymentService.processPayment()) .thenReturn("SUCCESS");

3. Spring Cloud Contract

• Defines contracts between microservices. • Ensures both producer and consumer follow the same API specification.

4. MockWebServer

• Lightweight HTTP server. • Useful for testing REST clients.

Testing Levels:

Unit Testing:

• Mock dependencies using Mockito. • No external systems involved.

Integration Testing:

• Mock external APIs using WireMock. • Test actual HTTP communication.

Contract Testing:

• Verify API compatibility between services. • Use Spring Cloud Contract.

Benefits:

• Faster test execution. • No dependency on external systems. • Stable and repeatable tests. • Easier testing of failure scenarios. • Reduced infrastructure requirements.

Real-World Example:

An E-Commerce Application:

Services: • Order Service • Inventory Service • Payment Service • Notification Service

While testing Order Service:

• Inventory Service is mocked. • Payment Service is mocked. • Notification Service is mocked.

This allows testing Order Service independently without running all microservices.

Failure Scenario Testing:

Mock a timeout:

stubFor(get(urlEqualTo("/payment"))

    .willReturn(
        aResponse()
        .withFixedDelay(10000)));

This helps verify:

• Retry logic • Circuit breakers • Fallback mechanisms

Best Practice:

• Use Mockito for unit tests. • Use WireMock for integration tests. • Use Spring Cloud Contract for consumer-driven contract testing. • Avoid calling real microservices during automated test execution.

Interview Tip: A concise interview answer is: To mock microservices during testing, I typically use Mockito for unit testing and WireMock for integration testing. These tools simulate external service behavior by returning predefined responses, allowing tests to run independently of actual microservices. This improves test reliability, speed, and enables testing of both success and failure scenarios without requiring external systems to be available.