liveBook · Manning

Application context

As explained in Testing Spring Boot Applications

Spring ApplicationContext is the Spring container that manages application components and provides access to configured beans.

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

How Testing Spring Boot Applications covers it

How deep it goes
Explains context creation, lifecycle and bean access through testing annotations, without descending into ApplicationContext implementation internals.
Examples
Shows a mocked external validator and injected repository, plus a repository assertion after an HTTP request.
What it assumes
Assumes familiarity with Spring beans and annotated tests; introduces the test-context lifecycle at a high level.
What you can do after
Operate/configure it with @SpringBootTest, @Autowired and selective overrides while avoiding unnecessary context variations.
How it connects
Connects dependency injection and beans to @SpringBootTest, Spring TestContext, @Autowired and test configuration.

Also explained in

Compare how 3 books explain application context →

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.