Spring boot test configuration
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Spring Boot test configuration is mainly about loading the right amount of application context for the test you are writing. The fastest and cleanest tests usually avoid loading the whole app unless they truly need it. Spring Boot gives you several layers of test configuration, from focused test slices to full integration contexts.
Start with the Smallest Test Context
A common mistake is using @SpringBootTest for every test class. That works, but it is often slow and unnecessary.
Use smaller slices when possible:
- '
@WebMvcTestfor controller-layer tests' - '
@DataJpaTestfor JPA repositories' - '
@JsonTestfor JSON serialization' - '
@RestClientTestfor REST client components'
Example for a controller test:
This loads only the web layer, not the whole application.
Use @SpringBootTest for Full Integration
When the test really needs the full application context, use @SpringBootTest.
This is appropriate for:
- startup wiring checks
- cross-layer integration tests
- tests that need the real Boot configuration
Just do not use it as the default for every test class.
Add Test-Only Beans with @TestConfiguration
When you need a bean only in tests, @TestConfiguration is a clean way to add or override it.
Then import it into the test:
This keeps test wiring explicit instead of smuggling test behavior into production config classes.
Override Properties for Tests
Spring Boot also makes property overrides easy.
For broader test environments, @ActiveProfiles("test") plus application-test.properties is often cleaner than repeating inline properties everywhere.
That is a strong pattern when many tests share the same test environment setup.
For infrastructure-style integration tests, newer Spring patterns such as @DynamicPropertySource are also useful when container ports or ephemeral service URLs are not known ahead of time. That keeps the test configuration dynamic without hardcoding values into static property files.
Mock at the Boundary You Actually Need
@MockBean is powerful, but overusing it can turn integration tests into brittle unit tests with a large application context. Mock only the dependencies that the current test truly needs to isolate.
If nearly every bean is mocked, that is a sign the test should probably be a plain unit test without Spring at all.
Keep Test Layers Intentional
A good rule of thumb is to decide first what the test is proving:
- business logic only: plain unit test, no Spring context
- controller mapping and HTTP behavior:
@WebMvcTest - repository behavior:
@DataJpaTest - full wiring across layers:
@SpringBootTest
That one decision usually determines most of the test configuration. Once the scope is explicit, the annotations become much easier to choose consistently.
Common Pitfalls
- Using
@SpringBootTestfor every test and paying unnecessary startup cost. - Loading a test slice and then expecting beans from unrelated layers to be present.
- Hiding test-only behavior in production configuration instead of
@TestConfiguration. - Overusing
@MockBeanuntil the test no longer resembles the real application wiring. - Mixing many property override styles without a clear convention.
Summary
- Choose the smallest Spring Boot test context that matches the test goal.
- Use test slices such as
@WebMvcTestor@DataJpaTestwhen full context is unnecessary. - Use
@SpringBootTestfor real integration cases, not as the default for everything. - Add test-only beans with
@TestConfigurationand override properties deliberately. - Good Spring Boot test configuration is mostly about reducing unnecessary context while keeping intent clear.
Related reading
- Spring boot Test fails saying, Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean
- spring boot test unable to inject TestRestTemplate and MockMvc
- Spring Boot TestConfiguration Not Overriding Bean During Integration Test
- Spring boot testing with liquibase fails
- Spring Boot Unit Test Autowired
- Spring Boot Unit Test ignores logging.level
- Spring boot Unable to start embedded Tomcat servlet container
- Spring Boot Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.