Describe how you would set up integration tests for a Spring Boot application that interacts with an external API.

Integration tests for a Spring Boot application that calls an external API use @SpringBootTest to load the full application context, combined with @MockBean to stub out the external API client so tests remain fast and independent of the real service's availability.

Key Points: • @SpringBootTest loads the complete Spring context, exercising the real wiring between components rather than an isolated slice. • @MockBean replaces the bean representing the external API client with a Mockito mock, so no real network call happens during the test. • Mocking at the client boundary lets the test verify how the application handles various API responses, including errors and timeouts, deterministically. • For deeper confidence, tools like WireMock can stub the actual HTTP layer instead of mocking the Java client, testing serialization and error handling more realistically. • Integration tests should cover both the happy path and failure scenarios, like the external API being down or returning malformed data.

Example: A test for an order-enrichment feature that calls a shipping-rates API mocks the ShippingRatesClient bean to return a fixed rate, then verifies the order service correctly applies that rate, without ever making a real HTTP call during the test run.

Code Example:

@SpringBootTest
class OrderServiceIntegrationTest {

    @MockBean
    private ShippingRatesClient shippingRatesClient;

    @Autowired
    private OrderService orderService;

    @Test
    void appliesShippingRate() {
        when(shippingRatesClient.getRate(any())).thenReturn(new Rate(9.99));
        Order order = orderService.createOrder(sampleRequest());
        assertEquals(9.99, order.getShippingCost());
    }
}

Interview Tip: A concise interview answer is:

"I'd use @SpringBootTest to load the full application context and @MockBean to stub out the external API client, so the test exercises real internal wiring without depending on the actual external service being up. I'd cover both successful responses and failure cases like timeouts, so the integration is verified end-to-end within the app's boundary."