This version is still in development and is not considered stable yet. For the latest stable version, please use Spring Framework 6.2.1! |
Context Management
Each TestContext
provides context management and caching support for the test instance
for which it is responsible. Test instances do not automatically receive access to the
configured ApplicationContext
. However, if a test class implements the
ApplicationContextAware
interface, a reference to the ApplicationContext
is supplied
to the test instance. Note that AbstractJUnit4SpringContextTests
and
AbstractTestNGSpringContextTests
implement ApplicationContextAware
and, therefore,
provide access to the ApplicationContext
automatically.
@Autowired ApplicationContext
As an alternative to implementing the
Similarly, if your test is configured to load a
Dependency injection by using |
Test classes that use the TestContext framework do not need to extend any particular
class or implement a specific interface to configure their application context. Instead,
configuration is achieved by declaring the @ContextConfiguration
annotation at the
class level. If your test class does not explicitly declare application context resource
locations or component classes, the configured ContextLoader
determines how to load a
context from a default location or default configuration classes. In addition to context
resource locations and component classes, an application context can also be configured
through application context initializers.
The following sections explain how to use Spring’s @ContextConfiguration
annotation to
configure a test ApplicationContext
by using XML configuration files, Groovy scripts,
component classes (typically @Configuration
classes), or context initializers.
Alternatively, you can implement and configure your own custom SmartContextLoader
for
advanced use cases.