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.
Suppose an Order Service calls a Payment Service to process payments.
Order Service
↓
Payment Service
Order Service
↓
WireMock Server
↓
Mock Response
The Order Service believes it is communicating with the real Payment Service, while WireMock returns predefined responses.
stubFor(post(urlEqualTo("/payments"))
.willReturn(
aResponse().withStatus(200)
.withBody(
{\"status\":\"SUCCESS\"}
)));
POST /payments
{
status": "SUCCESS
}
1. WireMock
2. Mockito
@Mock
private PaymentService paymentService;when(paymentService.processPayment())
.thenReturn("SUCCESS");
3. Spring Cloud Contract
4. MockWebServer
This allows testing Order Service independently without running all microservices.
stubFor(get(urlEqualTo("/payment"))
.willReturn(
aResponse()
.withFixedDelay(10000)));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.
Last updated: 11 November 2026
Previous question: How can you mock external services in a Spring Boot test?
Next question: Explain the process of creating a Docker image for a Spring Boot application.