Can Spring Boot test classes reuse application context for faster test run?
Data Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.
Spring Boot is a popular framework for building stand-alone, production-grade applications with the Spring ecosystem. Testing, an integral part of a healthy development process, is made easier and more efficient with Spring Boot's robust testing framework. In this article, we'll delve into how Spring Boot test classes can leverage the reuse of an application context to expedite test execution.
Understanding Spring Boot Application Context
In Spring Boot, the application context is a central container that manages beans' lifecycle and dependencies. It acts as a repository for all beans and configurations and is initialized during application startup. When testing, Spring Boot can create a test-specific application context, which mirrors the production context but is configured to be used for testing.
Reusing Application Context in Tests
Why Reuse the Context?
When testing, creating an application context is a resource-intensive operation. The application context must be loaded, all beans must be instantiated, and any configuration and initialization must run. Consequently, reloading this context for each test can considerably slow down the test suite.
Reusing the application context across tests can significantly improve test execution speed. Spring Boot facilitates this by caching the application context, thereby ensuring it is recreated only if absolutely necessary.
Enabling Context Reuse
Spring Boot reuses the application context by default. Test context caching is based on the configuration of your test class. Factors such as different application properties, profiles, or active beans may necessitate a new context, however, similar configurations will allow tests to share the same context. Here are some key considerations for optimizing context reuse:
- Same Configuration: Ensure that your test classes are annotated with the same configuration annotations (
@SpringBootTest,@ContextConfiguration, etc.). This will help Spring Boot to detect that the context can be reused. - Active Profiles: Use the same profiles across tests. Different profiles may lead to different bean definitions, requiring separate contexts.
- Different Context Requirements: Tests that require fundamentally different setups should be isolated. For example, tests relying on different database configurations might necessitate isolated contexts.
Example Configuration
Consider the following example to showcase context reuse:
- State Leakage: If tests modify beans' states, those changes persist across tests using the same context. It's essential to either avoid stateful beans or reset states manually in each test.
- Unexpected Behavior: If test failures occur due to state sharing, it may not be initially apparent that shared context caused the problem. Proper use of testing annotations and understanding the context lifecycle is crucial.
- Spring Boot's support annotations like
@DirtiesContextcan be used to mark tests that require a fresh context. This annotation can help in cases where context reuse is inappropriate. - Use the
@TestPropertySourceannotation to inject additional properties into the environment for tests when minor differences in configuration are needed without affecting reuse.
Related reading
- Can Tensorflow be used for global minimization of multivariate functions?
- Can the bigO of an algorithm be found programmatically by analyzing its perfs?
- Can there be an algorithm faster than linear search?
- Can we compute this in less than Onn ... nlogn or n
- Can twisted be implemented in Java?
- Can we instantiate an abstract class?
- Can you help me understand Moq Callback?
- Cannot find Assert.Fail and Assert.Pass or equivalent

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.