The Spring ApplicationContext is the container for an application's beans: configured component instances whose dependencies Spring resolves. Tests can use a context resembling the production application while changing selected properties or replacing components that cannot run locally. section 1.3
@SpringBootTest creates a test application context from Spring Boot configuration, runs it for the tests, and shuts it down afterward. Its SpringExtension connects JUnit to the Spring TestContext framework. This avoids manually assembling and managing a separate test application. section 3.1 section 1.3
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
}
A typical full-application test changes one property, injects beans for interaction and verification, and replaces an external validator with a mock. The annotations describe what differs from normal configuration rather than constructing the context by hand. section 1.4
Use @Autowired to access beans in the test context. For example, after an HTTP request selects a city, an injected repository can verify the stored selection. Direct repository access is convenient, but can couple a behavior test to the data model; prefer services or controllers where practical. section 2.4
A full context is not the only option: @SpringBootTest(classes = { … }) can limit testing to a subset, though that suits selected use cases. Properties and @TestConfiguration adjust the test application; @MockitoBean or @TestBean can replace or add beans. Keep configuration variations limited because many distinct contexts make tests slow and resource-intensive. section 3.6


