liveBook · Manning

Integration test

As explained in Spring Start Here, Second Edition

An integration test verifies behavior across multiple integrated components or systems.

An integration test checks that two or more parts of an application work together. Components can pass tests on their own yet fail when they communicate, so integration tests complement rather than replace unit tests. section 15.4 section 16.0

In a Spring application, the parts under test might be application objects, an object and a framework feature, or a repository and a database. A Spring integration test starts the Spring context so the test can check behavior with Spring managing the objects. This gives broader confidence but usually takes longer than an isolated unit test. section 16.1 section 16.4

Build an integration test around the interaction you want to check:

  • Set up the inputs and any controlled dependency behavior. section 15.4
  • Call the method or endpoint and let the selected components interact. section 16.1
  • Validate the result and the behavior that matters for that interaction. section 15.4

Keep the components whose interaction you want to test real. Other dependencies can still be replaced by mocks: controlled fake objects that let the test focus on a particular interaction. section 16.1 section 15.4

Figure 16.1 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.

The diagram shows a real service–repository interaction while another dependency is mocked. section 16.1

For a Spring integration test, @SpringBootTest starts the context, and @Autowired injects the object under test. Use @MockitoBean when a Spring bean should be replaced with a mock. section 16.4

Testing a transfer endpoint

A controller integration test can send a simulated transfer request with MockMvc, without starting a real HTTP server. Check the response status and, where relevant, the body or other details of the endpoint’s contract. The test can also check the resulting account balances while controlling a repository with a mock. section 16.2 section 16.4

Listing 16.4 Calling the endpoint with MockMvc
mockMvc.perform(post("/transfer")    #A
       .contentType(MediaType.APPLICATION_JSON)    #B
       .content("""    #C
               {
                 "senderAccountId": 1,
                 "receiverAccountId": 2,
                 "amount": 100
               }
               """))
       .andExpect(status().isOk());   #D

This request sends JSON to the transfer endpoint and checks for an OK response. section 16.2

How Spring Start Here, Second Edition covers it

How deep it goes
Explains test setup, execution, validation, dependency selection, and controller request checks through Spring examples.
Examples
Uses a money-transfer service and a simulated transfer endpoint to illustrate integration tests.
What you can do after
Apply integration testing via Spring Boot and MockMvc to check framework interactions and endpoint contracts.
How it connects
Connects integration testing to unit testing, mocks, dependency injection, persistence, and controller testing.

Also explained in

Compare how 40 books explain integration test →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Spring Start Here, Second Edition. The section links point to the parts of the book it draws on.