SpringBootTest vs ContextConfiguration vs Import in Spring Boot Unit Test
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Spring Boot and the broader Spring Framework ecosystem offer a range of annotations to help with unit testing, providing ways to configure the context in which the tests run. Among these, `@SpringBootTest`, `@ContextConfiguration`, and `@Import` are three primary annotations, each with distinct purposes and use-cases.
Understanding the Annotations
`@SpringBootTest`
`@SpringBootTest` is an annotation used for integration testing within the Spring Boot environment. It starts the entire Spring application context, suitable for tests that require the complete setup.
- Purpose: To test full Spring Boot app context.
- When to Use: Ideal for integration tests where you need the entire application context, including all beans and autoconfigurations.
- Pros:
- Provides a comprehensive test environment similar to production.
- Can be used with `webEnvironment` attribute to simulate a web environment.
- Cons:
- Slower execution due to the loading of full application context.
- Potentially overkill for unit tests that only need a fragment of the application.
- Purpose: To load the application context for unit tests with specific configurations.
- When to Use: Best for unit tests where only a specific subset of configurations is required.
- Pros:
- More efficient for targeted testing.
- Allows for precise control over the test context configuration.
- Cons:
- Requires more setup.
- Less suitable for tests requiring full context loading.
- Purpose: To bring specific configurations or beans into the test context.
- When to Use: Useful when you don’t need the entire context, but just specific components.
- Pros:
- Very efficient for modular testing.
- Simplifies testing by importing only what's necessary.
- Cons:
- Limited scope compared to `@SpringBootTest`.
- Can become complex when multiple dependencies are needed.
- Test Strategy: Selecting the right annotation for the job is critical, especially with large applications. While `@SpringBootTest` is the most comprehensive, it’s generally best to default to `@ContextConfiguration` and `@Import` for most unit tests to keep performance optimal.
Related reading
- SpringBootTest Vs WebMvcTest DataJpaTest service unit tests, what is the best?
- Springfox 3.0.0 is not working with Spring Boot 2.6.0
- Springfox swagger not working in spring boot 2.2.0
- Springfox Type javax.servlet.http.HttpServletRequest not present
- SpringRunner vs SpringBootTest
- SSD raw I/O benchmarks with random read/write
- Spring's RequestParam with Enum
- Sprintf equivalent in Java

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.