External services can be mocked in Spring Boot tests to avoid making real network calls during test execution. Mocking allows us to simulate the behavior of third-party APIs, microservices, databases, or external systems, making tests faster, more reliable, and independent of external dependencies.
Suppose an Order Service communicates with a Payment Service. During testing, instead of calling the real Payment Service, we can mock its response and verify how the Order Service behaves.
@SpringBootTest
class OrderServiceTest {
@MockBean
private PaymentService paymentService;
@Autowired
private OrderService orderService;
@Test
void testPlaceOrder() {
Mockito.when(
paymentService.processPayment())
.thenReturn("SUCCESS");
String result =
orderService.placeOrder();
assertEquals(
"ORDER_CREATED",
result);
}
}Application Context
↓
Real PaymentService Bean
↓
Replaced By
↓
Mock PaymentService Bean
Alternative Tool: WireMock
WireMock is commonly used when testing REST API integrations.
External API Call
↓
WireMock Server
↓
Mock Response
1. @MockBean + Mockito
2. WireMock
Order Service
↓
Payment Service
↓
Inventory Service
This allows the Order Service to be tested independently without requiring other services to be running.
A concise interview answer is: In Spring Boot, external services can be mocked using @MockBean, which replaces the actual bean in the Spring context with a Mockito mock. For REST-based integrations, tools like WireMock can simulate real HTTP responses. This approach makes tests faster, reliable, and independent of external service availability.
Last updated: 1 November 2026
Previous question: What is Spring Boot DevTools used for?
Next question: How do you mock microservices during testing?