liveBook · Manning

@SpringBootTest

As explained in Testing Spring Boot Applications

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

@SpringBootTest marks a test class that needs a Spring Boot application context. It finds the application configuration, starts a test application before the tests run, and makes its beans available for injection. Use it when a test needs several application components working together, rather than one isolated class. section 2.8 section 3.1

The annotation works declaratively: test code does not call a method to start the application. Behind it, a JUnit SpringExtension connects the test to Spring’s TestContext framework, while a Boot-specific bootstrapper creates the context. Attributes such as properties and classes let a test adjust its configuration. section 1.1 section 3.1

Listing 1.4 A typical Spring Boot Tests configuration.
@SpringBootTest(properties = "widget.id.step=5") #1
@AutoConfigureMockMvc #2
class AwesomeSpringBootTests {

  @Autowired
  private MockMvc mockMvc; #2

  @Autowired
  private WidgetRepository repository; #3

  @MockitoBean
  private WidgetValidator mockValidator; #4


}

This configuration changes a property only for the test, injects a repository for verification, and replaces an external validator with a Mockito bean. @AutoConfigureMockMvc supplies MockMvc for exercising the web layer. section 1.4

From startup check to endpoint test

An empty contextLoads() test checks that the application starts, including its bean dependencies and property binding; it does not check business behavior. To test an endpoint, add @AutoConfigureMockMvc, inject MockMvcTester, request a path such as /, and assert the response status and body. section 2.2 section 2.3

Choose the web environment and test scope deliberately:

  • The default MOCK web environment supports HTTP-like MVC tests without starting a web server. Use RANDOM_PORT with a test client when the behavior under test requires a real server, such as error dispatch or session handling. section 4.1 section 4.6
  • Do not load the whole application for every case. For many variations of self-contained logic, construct the class directly in a unit test; full-context tests become harder to manage as the application grows. section 2.7

How Testing Spring Boot Applications covers it

How deep it goes
Explains lifecycle and injection through JUnit and TestContext, then identifies configuration attributes without tracing their full implementation.
Examples
Uses an empty startup test, a configured application test, and an HTTP endpoint assertion.
What it assumes
Assumes experience writing automated tests and works from an existing Spring Boot application.
What you can do after
Apply it via @SpringBootTest to configure integration tests and choose between mock MVC and a real server.
How it connects
Connects JUnit, Spring TestContext, dependency injection, Mockito beans, and MockMvcTester.

Also explained in

Compare how 4 books explain @SpringBootTest →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Testing Spring Boot Applications. The section links point to the parts of the book it draws on.