Spring TestContext Framework connects tests to Spring application contexts. It assembles a test application, starts it before execution, makes it available for injection, and shuts it down afterward. Spring Boot commonly reaches it through @SpringBootTest and SpringExtension. section 3.1
@SpringBootTest is a meta-annotation that registers the JUnit extension, selects Spring Boot test bootstrapping, and exposes configuration controls. Its defaults use a mock web environment rather than starting an embedded server; parameters can change properties, configuration classes, web environment, arguments, and main-method usage. section 3.1
Listing 3.1 Simplified code for @SpringBootTest extension.
@ExtendWith(SpringExtension.class) #1
@BootstrapWith(SpringBootTestContextBootstrapper.class) #2
public @interface SpringBootTest {
// Below are generic Spring configuration options, exposed by @SpringBootTest
String[] properties() default {}; #3
Class<?>[] classes() default {}; #3
// Below are Spring Boot specific configuration options
WebEnvironment webEnvironment() default WebEnvironment.MOCK; #3
UseMainMethod useMainMethod() default UseMainMethod.NEVER; #3
String[] args() default {}; #3
}
Use the simplified annotation listing to trace how SpringExtension, SpringBootTestContextBootstrapper, and test configuration options combine. section 3.1
The framework also caches ApplicationContext instances between tests. Tests with matching properties, profiles, configuration classes, and beans can reuse one running context, avoiding repeated startup costs. Many configuration variations instead create separate contexts and increase memory, CPU, and runtime overhead. section 3.5 section 3.5.1
Observing context reuse
Two nested @SpringBootTest classes can share one context when their configuration is identical. A deliberately slow bean starts once, while the second test reuses the cached application. Enable DEBUG logging for org.springframework.test to inspect cache size, hits, misses, and failures. section 3.5.1
Use the framework selectively. @Autowired, @Qualifier, and @Value expose test-application state; @TestConfiguration, @MockitoBean, and @TestBean support controlled additions or external-service stubbing. Prefer focused configurations when appropriate, and avoid unnecessary application variants. section 3.6

