Spring Boot Testing: @SpringBootTest, Slices, and Context Caching
Fast recall
Quick revision
Core answer
@SpringBootTest loads the full application context and is suitable for integration tests, but it is slow. Test slice annotations like @WebMvcTest, @DataJpaTest, and @JsonTest load only the slice of the context relevant to the test (controllers, JPA layer, JSON serialization), making them much faster. Spring Test caches `ApplicationContext`s across test classes that share identical configuration — so the expensive context startup happens once per unique configuration combination. The cache key includes @MockBean declarations, @ActiveProfiles, and @TestPropertySource — variation in any of those creates a separate context.Whether the candidate understands the cost of `@SpringBootTest` for every test and can choose the right slice for unit-like speed with integration-like confidence
Using `@SpringBootTest` for every test including repository-only or controller-only tests, causing very slow test suites, or accidentally breaking context caching by adding test-specific `@MockBean` in a way that creates a unique context per test class
Explain that `@MockBean` creates a new `ApplicationContext` (breaking caching) so it should be standardized in a base class or `@TestConfiguration`, and mention the cache logging at `org.springframework.test.context.cache=DEBUG`
Interview-ready guide
Interview answer
Spring Boot provides test slice annotations that load only the beans relevant to what you are testing. Using @SpringBootTest for everything works but is unnecessarily slow — a repository test that starts the web layer and message brokers wastes seconds on every CI run.
Test slices
@WebMvcTest(OrderController.class)— loads only MVC infrastructure, the specified controller, and related@ControllerAdvice; no database, no service layer; use@MockBeanfor dependencies@DataJpaTest— loads only JPA/Hibernate + an embedded H2 database; no web layer; tests repository logic in isolation@SpringBootTest— loads the fullApplicationContext; use for end-to-end integration tests only
Context caching
- Spring caches
ApplicationContextinstances across test classes that share the same configuration @MockBeanis the most common cache-buster — each unique set of mocked beans creates a distinct context- Extract shared mocks into a base test class or
@TestConfigurationclass to maximize cache hits across the test suite - Enable
org.springframework.test.context.cache=DEBUGto see cache hits and misses
Embedded vs real database
@DataJpaTestdefaults to H2; if your query uses a PostgreSQL-specific function, the test passes with H2 and fails in production@AutoConfigureTestDatabase(replace = NONE)+ Testcontainers runs the real database in CI — the only reliable approach for dialect-specific queries
In an interview, the strong answer is: slices for fast focused tests, full `@SpringBootTest` for end-to-end paths, and a base class to keep mock signatures consistent so the cache actually works.
Spring Boot Testing: @SpringBootTest, Slices, and Context Caching
Direct Answer
@SpringBootTest loads the full application context and is suitable for integration tests, but it is slow. Test slice annotations like @WebMvcTest, @DataJpaTest, and @JsonTest load only the slice of the context relevant to the test (controllers, JPA layer, JSON serialization), making them much faster. Spring Test caches ApplicationContexts across test classes that share identical configuration — so the expensive context startup happens once per unique configuration combination. The cache key includes @MockBean declarations, @ActiveProfiles, and @TestPropertySource — variation in any of those creates a separate context.
Common Mistakes & Pitfalls
Using @SpringBootTest for every test including repository-only or controller-only tests, causing very slow test suites, or accidentally breaking context caching by adding test-specific @MockBean in a way that creates a unique context per test class
Check your understanding
Answer these without looking back at the article.
- How do you use Testcontainers with `@DataJpaTest` to test against a real PostgreSQL database?
- Why does adding `@MockBean` to a `@SpringBootTest` class sometimes make the test suite slower?
- What is the difference between `@SpringBootTest` `webEnvironment` MOCK and RANDOM_PORT?
- When is `@DirtiesContext` justified, and what is a better alternative when it isn't?
Related interview prep categories
1Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.