This version is still in development and is not considered stable yet. For the latest stable version, please use Spring Framework 6.1.14!

@ContextConfiguration

@ContextConfiguration is an annotation that can be applied to a test class to configure metadata that is used to determine how to load and configure an ApplicationContext for integration tests. Specifically, @ContextConfiguration declares the application context resource locations or the component classes used to load the context.

Resource locations are typically XML configuration files or Groovy scripts located in the classpath, while component classes are typically @Configuration classes. However, resource locations can also refer to files and scripts in the file system, and component classes can be @Component classes, @Service classes, and so on. See Component Classes for further details.

The following example shows a @ContextConfiguration annotation that refers to an XML file:

  • Java

  • Kotlin

@ContextConfiguration("/test-config.xml") (1)
class XmlApplicationContextTests {
	// class body...
}
1 Referring to an XML file.
@ContextConfiguration("/test-config.xml") (1)
class XmlApplicationContextTests {
	// class body...
}
1 Referring to an XML file.

The following example shows a @ContextConfiguration annotation that refers to a class:

  • Java

  • Kotlin

@ContextConfiguration(classes = TestConfig.class) (1)
class ConfigClassApplicationContextTests {
	// class body...
}
1 Referring to a class.
@ContextConfiguration(classes = [TestConfig::class]) (1)
class ConfigClassApplicationContextTests {
	// class body...
}
1 Referring to a class.

As an alternative or in addition to declaring resource locations or component classes, you can use @ContextConfiguration to declare ApplicationContextInitializer classes. The following example shows such a case:

  • Java

  • Kotlin

@ContextConfiguration(initializers = CustomContextInitializer.class) (1)
class ContextInitializerTests {
	// class body...
}
1 Declaring an initializer class.
@ContextConfiguration(initializers = [CustomContextInitializer::class]) (1)
class ContextInitializerTests {
	// class body...
}
1 Declaring an initializer class.

You can optionally use @ContextConfiguration to declare the ContextLoader strategy as well. Note, however, that you typically do not need to explicitly configure the loader, since the default loader supports initializers and either resource locations or component classes.

The following example uses both a location and a loader:

  • Java

  • Kotlin

@ContextConfiguration(locations = "/test-context.xml", loader = CustomContextLoader.class) (1)
class CustomLoaderXmlApplicationContextTests {
	// class body...
}
1 Configuring both a location and a custom loader.
@ContextConfiguration("/test-context.xml", loader = CustomContextLoader::class) (1)
class CustomLoaderXmlApplicationContextTests {
	// class body...
}
1 Configuring both a location and a custom loader.
@ContextConfiguration provides support for inheriting resource locations or configuration classes as well as context initializers that are declared by superclasses or enclosing classes.

See Context Management, @Nested test class configuration, and the @ContextConfiguration javadocs for further details.