The @Import annotation, from org.springframework.context.annotation, adds specific configuration classes to the Spring context. In tests it lets you load only the beans a test needs, for example a security configuration in a @WebMvcTest, without starting the whole application with @SpringBootTest. Tests start faster and fail for clearer reasons.
You can put @Import on a @Configuration class or, in Spring Boot tests, directly on the test class.
@Import takes one or more classes and registers them with the application context. The classes can be:
@Configuration classes, so their @Bean methods are processed.ImportSelector implementations, which choose classes to import at runtime.ImportBeanDefinitionRegistrar implementations, which register bean definitions programmatically.In production code it composes configuration. In tests its main value is adding things that the test slice or component scan does not load by itself.
Spring Boot test slices such as @WebMvcTest and @DataJpaTest load only a part of the application, for example controllers and MVC infrastructure. Beans outside that part, such as your own security rules or a helper component, are not loaded. @Import adds them.
Here @WebMvcTest loads the controller and MVC infrastructure, @Import adds SecurityConfig, and @MockitoBean replaces the service with a Mockito mock. @MockitoBean is available since Spring Framework 6.2 and replaces the older @MockBean, which Spring Boot deprecated in version 3.4.
Define test-only beans in a @TestConfiguration class and import it where needed.
A static nested class annotated with @TestConfiguration inside the test class is picked up automatically. A top-level @TestConfiguration class is not found by component scanning, so you must add it with @Import. That is also how you share one test configuration between several test classes.
| Annotation | What it does | Typical use |
|---|---|---|
@Import | Adds named classes to the existing context setup | Add a few beans to a slice or @SpringBootTest |
@ContextConfiguration(classes = ...) | Defines the configuration for the test, replacing default detection | Plain Spring tests without Boot |
@SpringBootTest(classes = ...) | Chooses the configuration or main class to start | Full or customised context |
@TestPropertySource | Overrides properties | Test-specific settings |
@MockitoBean, @MockBean | Replaces or adds a bean as a mock | Isolate collaborators |
@ContextConfiguration tells Spring exactly which classes make up the context. @Import is additive and is the idiomatic choice in Spring Boot tests.
You can also build a small context without Boot slices.
Here PriceCalculator and TaxConfig are your own classes. Only the two imported classes become beans, so the test does not depend on the rest of the application.
@Import over @SpringBootTest when you test one layer.@Import sets create different contexts, so many variations slow the suite.NoSuchBeanDefinitionException, check whether the class needs to be imported or is excluded by the slice.Yes, in Spring Boot tests such as @SpringBootTest and the slice annotations. The imported classes are added to the test context.
@ContextConfiguration defines which classes make up the context, while @Import adds classes to the existing configuration. In Boot tests, @Import is usually enough.
A top-level @TestConfiguration is not component scanned. Add it with @Import(MyTestConfig.class). Nested static ones are detected automatically.
Yes. You can import a plain class and Spring registers it as a bean, so you do not need @Component or scanning for it.
In tests, @Import adds exactly the configuration classes or components you name to the context. Use it with slice tests to add security or helper beans, combine it with @TestConfiguration for test-only overrides such as a fixed clock, and remember that top-level test configurations need an explicit import. Keep the imported set small so tests stay fast and contexts stay cacheable.