Overview

16 Implementing integration tests for Spring apps

Integration tests are presented as a way to check whether separate parts of a Spring application work together correctly, rather than only proving that each class works alone. The chapter explains that real applications depend on Spring-managed behavior such as dependency injection, configuration, transactions, security, repositories, and web request handling, so testing only isolated methods can miss failures in communication between components. The banking money transfer example is used to show how a test can verify collaboration between a service and a repository, or between a controller and the rest of the application flow.

The text shows how a Spring integration test is built by starting the application context and letting Spring manage the objects under test. A mock can still be used when the goal is not to verify a specific dependency, and the chapter highlights that the repository does not always need to be mocked unless that interaction is the focus of the test. It also emphasizes that using a real database is often unnecessary for these tests, since in-memory databases or containerized test databases can keep tests faster and less dependent on external infrastructure. The main idea is that unit tests remain best for fast checks of isolated logic, while integration tests confirm that the same logic behaves correctly when Spring features are actually involved.

The chapter then turns to controller integration tests, which validate the web contract exposed by an application. These tests send HTTP requests to endpoints and check behavior such as URL mapping, request body handling, response status, validation, headers, and sometimes security. Using a test client like MockMvc, the examples show how to simulate a POST request with JSON content and confirm both the HTTP response and the side effects on application state. The overall recommendation is a balanced strategy: many unit tests for quick feedback, targeted Spring integration tests for important framework interactions, and controller tests for critical API endpoints.

In a unit test’s case, all the dependencies needed to be mocked. If the integration test’s purpose is to verify how the TestService and AccountRepository interact, the repository can be the real object. An integration test can still mock an object if its purpose doesn’t verify the integration with a specific component.
A controller integration test. A tool such as MockMvc lets the test execute the web layer without starting a real server. The test sends a simulated HTTP request to the controller, while the developer decides which objects in the request-processing flow are real and which ones are replaced with mocks.

Summary

  • Integration tests validate how multiple parts of an application work together.
  • In a Spring app, integration tests help you check how your code works with framework capabilities such as dependency injection, configuration, transactions, persistence, security, and web endpoints.
  • Spring integration tests start the Spring context, allowing Spring to create and manage the beans as it does when the application runs. This way it also make the test run slower than a simple unit test.
  • You use @SpringBootTest to define a Spring integration test and @Autowired to inject the object whose behavior you test.
  • You use @MockitoBean to replace a Spring bean with a mock when the test needs to control that dependency.
  • In an integration test, you do not always have to mock all dependencies. You can use real objects when the purpose of the test is to validate how those objects interact.
  • Controller integration tests validate the contract of web endpoints, including the URL, HTTP method, request data, response status, response body, headers, cookies, and validation behavior.
  • Spring provides several tools for controller integration tests, such as:
    • MockMvc, which tests Spring MVC controllers without starting a real HTTP server
    • TestRestTemplate, which sends real HTTP requests to an application started on a test port
    • WebTestClient, which is often used for reactive applications
  • MockMvc lets you define request details such as content type, request body, parameters, headers, and cookies, and then validate the response.
  • A balanced testing strategy uses many unit tests for isolated logic, fewer Spring integration tests for framework interactions, and targeted controller integration tests for important endpoints.

FAQ

What is an integration test?An integration test checks how multiple parts of an application work together, instead of verifying one method or class in isolation.
What is a Spring integration test?A Spring integration test verifies that your code works correctly together with Spring framework features such as dependency injection, configuration, transactions, security, or persistence.
Does an integration test always need to mock all dependencies?No. Unlike unit tests, integration tests do not have to mock every dependency. They can use real objects when the goal is to test how components collaborate.
What does the @SpringBootTest annotation do?@SpringBootTest tells Spring Boot to start the application context for the test, so the test can use the application’s beans and configuration.
What is the purpose of @MockitoBean?@MockitoBean creates a Mockito mock and adds it to the Spring application context, so the test can replace a real bean with a controlled mock.
What is a controller integration test?A controller integration test checks how an endpoint behaves from the outside by sending HTTP requests and verifying the response, including status, body, headers, validation, and mapping.
What is MockMvc?MockMvc is a testing tool that lets you call Spring MVC controllers without starting a real HTTP server.
How do you configure MockMvc in a Spring test?Usually by autowiring the WebApplicationContext and building MockMvc with MockMvcBuilders.webAppContextSetup(context).build() before each test.
Why are controller integration tests useful?They help catch problems that unit tests may miss, such as wrong endpoint mappings, bad JSON conversion, missing validation, or unexpected response structure.
How are unit tests, Spring integration tests, and controller integration tests different?Unit tests check isolated logic quickly, Spring integration tests verify interaction with Spring features, and controller integration tests verify the HTTP endpoint contract seen by API clients.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Spring Start Here, Second Edition ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Spring Start Here, Second Edition ebook for free