@ContextConfiguration is a Spring TestContext Framework annotation that tells a test which configuration classes or XML files to use when it builds the ApplicationContext. The context is created once, cached, and injected into the test so @Autowired fields work.
You need it for plain Spring tests that run without Spring Boot. In a Spring Boot application, @SpringBootTest normally replaces it, because Boot finds your @SpringBootConfiguration automatically.
When a test class runs with the Spring extension, the TestContext Framework must know how to create the application context. @ContextConfiguration supplies that information. You point it at one or more @Configuration classes (the classes attribute) or at XML or Groovy files (the locations attribute).
The annotation also controls context inheritance and which ApplicationContextInitializer or ContextLoader is applied. If you declare no attributes, Spring looks for default configuration, such as static nested @Configuration classes inside the test class.
With JUnit 5 you enable Spring through @ExtendWith(SpringExtension.class). The example below loads a small configuration and injects a bean.
Only the beans declared in GreetingConfig exist in this context, so the test starts fast and does not depend on the rest of the application.
Legacy projects can still point to XML files. A path without a prefix is resolved relative to the test class package; use classpath: for an absolute classpath location.
You can list several files or classes in one annotation, and Spring merges them into a single context.
| Attribute | Use |
|---|---|
classes | One or more @Configuration or component classes |
locations / value | XML or Groovy resource paths |
initializers | ApplicationContextInitializer implementations that customise the context before refresh |
inheritLocations | Whether a subclass merges the parent test's configuration (default true) |
inheritInitializers | Same idea for initializers (default true) |
loader | Custom ContextLoader or SmartContextLoader |
Combine it with @ActiveProfiles("test") to activate profile-specific beans, and with @TestPropertySource to override properties for one test class. Because the annotation is inherited, a base test class can declare the shared configuration and subclasses add more.
Spring caches the context across test classes that use the same configuration, which is why a large suite stays fast. The cache key includes the configuration classes, locations, initializers, active profiles and property sources. Any difference creates a new context.
If a test changes bean state in a way that would affect others, add @DirtiesContext to that class or method so the context is closed and rebuilt. Use it sparingly, because every rebuild costs time.
In Spring Boot, @SpringBootTest searches upward for a class annotated with @SpringBootConfiguration, loads full auto-configuration and supports a random port web environment. Use @ContextConfiguration when you want a deliberately small context with explicit classes, or when the project does not use Boot.
Slice annotations such as @WebMvcTest and @DataJpaTest also bring their own configuration, so you rarely combine them with @ContextConfiguration. If you do, you replace Boot's default detection, which often hides auto-configuration you expected.
Usually not. @SpringBootTest detects your main configuration class automatically. Add @ContextConfiguration(classes = ...) only when you want to limit the context to specific classes.
classes takes Java configuration or component classes, while locations takes XML or Groovy resource paths. You can use both in one annotation if you need to.
The bean is probably not part of the context that @ContextConfiguration loads. Add the missing configuration class to classes, or use @Import on your test configuration.
Declare identical configuration on each test class. Spring caches contexts by configuration, so equal settings share one instance.
@ContextConfiguration defines which configuration builds the ApplicationContext for a Spring test. Use classes for Java config and locations for XML, pair it with SpringExtension on JUnit 5, and rely on context caching for speed. In Boot projects, prefer @SpringBootTest or a test slice unless you need a hand-picked context.