liveBook · Manning

Spring TestContext Framework

As explained in Testing Spring Boot Applications

Spring TestContext Framework is a Spring testing framework that manages application contexts and integrates Spring configuration and lifecycle support with test execution.

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

How Testing Spring Boot Applications covers it

How deep it goes
Descends from annotation composition into bootstrapping, context caching, configuration matching, and resource trade-offs.
Examples
Demonstrates nested @SpringBootTest classes, a slow bean, cache logging, and ApplicationContext hit and miss statistics.
What it assumes
Assumes familiarity with Spring Boot tests and annotations while clarifying extension registration, bootstrapping, and context reuse.
What you can do after
Operate Spring integration tests with controlled contexts, configurable bootstrapping, dependency injection, and cache-aware test suites.
How it connects
Connects Spring Boot, JUnit extensions, ApplicationContext, dependency injection, test configuration, and mocking.

Also explained in

Compare how 2 books explain Spring TestContext Framework →

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.