A Spring bean is an application component managed by the Spring ApplicationContext. The context connects components and lets application code or tests access them. In a Spring Boot test, the useful question is often which beans should remain as configured and which need test-specific replacements. section 1.3 section 1.4
A typical full-application test handles beans declaratively:
- Use @SpringBootTest to test the application with test-specific properties. section 1.4
- Use @Autowired to inject a bean, such as a repository, into the test for actions or assertions. section 1.4
- Use @MockitoBean to replace a production collaborator that should not run during the test. section 1.4
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
}
The test configuration injects a repository, replaces a validator with a mock, and makes MockMvc available without manually managing the application context. section 1.4
Direct bean access can make an integration test easier to write: after an HTTP request, an injected repository can check what was stored. It can also couple a behavior test to the data model. For real projects, prefer interacting through services or controllers when that avoids such coupling. section 2.4




