Chapter 16: Implementing integration tests for Spring apps
This introduction explains what Spring integration tests check and why they complement unit tests.
After this chapter you will be able to
- Unit and integration test scope. Distinguish a unit test's check of a small piece of code in isolation from an integration test's check of how dependent parts work together.
- Spring framework collaboration. Explain that a Spring integration test checks application logic while relevant Spring-provided capabilities are involved, and give an example of parts whose collaboration it could verify.
- Integration Testing Purpose. Explain why an app needs integration tests even when its components pass tests in isolation, and identify the kinds of interactions this section proposes testing.
- Integration Test Dependency Choices. Describe how an integration test follows the familiar test structure while choosing real or mocked dependencies according to the interaction being tested.
- Spring Managed Test Context. Explain what Spring manages in a Spring integration test and how a mocked dependency can participate in that context.
- Unit And Integration Test Roles. Justify using unit tests for component logic and Spring integration tests for necessary framework interactions rather than repeating every logic scenario as an integration test.
- Endpoint contract verification. Explain why a controller integration test sends a request through a web endpoint rather than testing only a Java method, and identify the client-visible failures it can catch.
- MockMvc client setup. Describe how the example prepares MockMvc with the application’s web context, and distinguish it from the other HTTP test clients mentioned.
- Controlled transfer test dependencies. Trace how the transfer test controls repository behavior while retaining the Spring web setup, and state what application effect it verifies.
- Transfer request and response assertions. Trace the MockMvc request to /transfer, explaining the JSON content type, request body, and status assertion, and describe how other client-visible details could also be tested.
What it builds on
- Implementing unit tests in Spring apps (chapter 15) · Prior work on implementing unit tests.
- Using Spring Data JDBC (chapter 14) · Assumes familiarity with the app’s repository and persisted data.
- Using the @Autowired annotation to inject beans (chapter 3) · Assumes familiarity with @Autowired injection.
- Implementing REST endpoints (chapter 10) · REST endpoints expose application functionality to clients
- Using a request body to get data from the client (chapter 10) · request bodies supply data to REST endpoints
Section by section
Introduction
This introduction explains what Spring integration tests check and why they complement unit tests.
- Unit and integration test scope: Distinguish a unit test's check of a small piece of code in isolation from an integration test's check of how dependent parts work together.
Why it matters: This distinction helps you choose what kind of behavior a test needs to check. - Spring framework collaboration: Explain that a Spring integration test checks application logic while relevant Spring-provided capabilities are involved, and give an example of parts whose collaboration it could verify.
Why it matters: A passing test for each class alone cannot confirm that the application works with the Spring features it uses. - Complementary roles of test types: Explain why integration tests do not replace unit tests: unit tests provide fast feedback on small independent logic, while integration tests provide confidence that the application works as a Spring application.
Why it matters: Keeping both kinds of tests preserves fast checks of individual logic and checks of the assembled application.
Key terms: unit test, integration test, isolation, Spring integration test, application logic, Spring framework, unit tests, integration tests, fast feedback
16.1 Implementing Spring integration tests
This section explains what integration tests check in Spring apps, how they differ from unit tests, and when to use real dependencies or mocks.
- Integration Testing Purpose: Explain why an app needs integration tests even when its components pass tests in isolation, and identify the kinds of interactions this section proposes testing.
Why it matters: Passing isolated tests cannot show whether the parts of an app work together. - Integration Test Dependency Choices: Describe how an integration test follows the familiar test structure while choosing real or mocked dependencies according to the interaction being tested.
Why it matters: The dependency choice determines which interaction the test can actually verify. - Test Database Isolation: Justify using an in-memory or containerized local database when an integration test calls a real repository.
Why it matters: A test of application integration should not fail merely because external infrastructure is slow or unavailable. - Spring Managed Test Context: Explain what Spring manages in a Spring integration test and how a mocked dependency can participate in that context.
Why it matters: With Spring managing the tested object, a test can catch broken framework integration that an isolated unit test would miss. - Unit And Integration Test Roles: Justify using unit tests for component logic and Spring integration tests for necessary framework interactions rather than repeating every logic scenario as an integration test.
Why it matters: Separating the two jobs preserves coverage without paying the cost of starting Spring for every logic scenario.
Key terms: integration tests, persistence layer, assumptions, mock, repository, real object, in-memory database, H2, Testcontainers, infrastructure
16.2 Implementing controller integration tests
Explains why controller integration tests check an endpoint from a client’s perspective, then demonstrates a MockMvc test for a money-transfer endpoint and places it in a balanced test strategy.
- Endpoint contract verification: Explain why a controller integration test sends a request through a web endpoint rather than testing only a Java method, and identify the client-visible failures it can catch.
Why it matters: A correct service is not enough if clients cannot use its endpoint as expected. - MockMvc client setup: Describe how the example prepares MockMvc with the application’s web context, and distinguish it from the other HTTP test clients mentioned.
Why it matters: The test client determines how the test reaches the controller and how much of a real HTTP exchange it uses. - Controlled transfer test dependencies: Trace how the transfer test controls repository behavior while retaining the Spring web setup, and state what application effect it verifies.
Why it matters: Controlling the repository keeps this example focused while still checking what the endpoint does to the application flow. - Transfer request and response assertions: Trace the MockMvc request to /transfer, explaining the JSON content type, request body, and status assertion, and describe how other client-visible details could also be tested.
Why it matters: Request and response assertions show whether clients can send the expected data and receive the expected HTTP result. - Balanced test category roles: Distinguish unit, Spring integration, and controller integration tests by what each verifies and justify the section’s suggested balance among them.
Why it matters: Using the three test types together provides fast feedback without leaving Spring behavior or client-facing contracts unchecked.
Key terms: controller integration test, endpoint contract, @SpringBootTest, WebApplicationContext, MockMvc, MockMvcBuilders.webAppContextSetup(context).build(), TestRestTemplate, WebTestClient, @MockitoBean, AccountRepository
16.3 Questions
This section provides self-check questions about integration testing in Spring, with answers in Appendix A.
16.4 Summary
The section summarizes what Spring integration and controller tests validate, how they are set up, and how they fit into a balanced testing strategy.
Concepts explained in this chapter
- integration testAn integration test verifies behavior across multiple integrated components or systems.
- @MockitoBeanA Spring Test annotation that registers a Mockito mock in the test application context, replacing an existing bean when one matches.
- @SpringBootTest@SpringBootTest is a Spring Boot test annotation that loads an application context for integration testing.
- MockMvcMockMvc is a Spring testing API that performs requests against Spring MVC controllers without starting a real HTTP server.
- controller integration testA controller integration test verifies a web controller's endpoint contract, including request handling and response behavior.
- WebApplicationContextWebApplicationContext is a Spring application context that contains web-layer configuration, controllers, and their required components.
- endpoint contractAn endpoint contract is the specification of a web endpoint's URL, HTTP method, inputs, outputs, status, headers, and validation behavior.
- payloadAn HTTP request body is the payload portion of a request that carries data sent by the client.
- unit testA unit test checks the behavior of a small, isolated unit of code.
These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.
