@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



