liveBook · Manning

@SpringBootTest

As explained in Spring Start Here, Second Edition

@SpringBootTest is a Spring Boot test annotation that loads an application context for integration testing.

@SpringBootTest starts the Spring application context for a test, making application beans and configuration available. This supports integration tests that check how application parts work together. section 16.2 section 16.4

For a controller integration test, combine it with an injected WebApplicationContext and MockMvc. MockMvc sends simulated requests through the web setup without starting a real server. You can replace selected beans with mocks while keeping the rest of that setup in use. section 16.2

Listing 16.2 Configuring a MockMvc object
@SpringBootTest    #A 
public class TransferMoneyTests {
 
  @Autowired
  private WebApplicationContext context;     #B
 
  private MockMvc mockMvc;
 
  @BeforeEach
  void setup() {
    this.mockMvc = 
       MockMvcBuilders.webAppContextSetup(context).build();      #C
  }
 
}

See how @SpringBootTest, WebApplicationContext, and MockMvc fit together in a small test-class setup. section 16.2

Test a money transfer endpoint

A controller integration test can send a simulated POST request to /transfer with MockMvc and check for an OK response. Mocking AccountRepository lets the test control repository behavior while exercising the Spring web setup. section 16.2

How Spring Start Here, Second Edition covers it

How deep it goes
Defines its context-loading role and demonstrates it in a MockMvc controller test, without detailing annotation options.
Examples
Demonstrates a money transfer endpoint test using MockMvc and a mocked AccountRepository.
What it assumes
Assumes an application with Spring beans and configuration, and defines how the annotation loads them for tests.
What you can do after
Apply it with MockMvc to test a web endpoint through the Spring context and verify its response.
How it connects
Connects Spring integration tests with dependency injection, WebApplicationContext, MockMvc, and @MockitoBean.

Also explained in

Compare how 4 books explain @SpringBootTest →

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.