What are the best practices for integration testing in Spring MVC?

Integration testing in Spring MVC verifies that multiple layers of the application — controllers, services, repositories, and configuration — work correctly together, as opposed to unit tests that isolate a single class. Doing this well means realistic tests that don't become slow, flaky, or destructive to shared data.

Key Points

Example

A checkout flow integration test might use @SpringBootTest with an embedded H2 database, send a real HTTP POST through TestRestTemplate to place an order, and verify both the response and that the order was actually persisted, with the transaction rolled back afterward.

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class OrderIntegrationTest {

    @Autowired
    private TestRestTemplate restTemplate;

    @Test
    void createsOrder() {
        ResponseEntity<Order> response =
                restTemplate.postForEntity("/orders", new OrderRequest(...), Order.class);

        assertEquals(HttpStatus.CREATED, response.getStatusCode());
    }
}

Interview Tip

A concise interview answer is

For Spring MVC integration tests, I use @SpringBootTest with MockMvc or TestRestTemplate to exercise real HTTP flows across controllers, services, and the database. I keep tests isolated with a dedicated test profile and an embedded or containerized database, and roll back or clean up data after each test so runs stay independent.

Last updated: 16 July 2026

Previous question: How can you mock Spring MVC dependencies for unit testing?

Next question: Explain how Spring MVC supports file upload.

More Spring MVC (Optional) interview questions

All Spring MVC (Optional) questions