|
This version is still in development and is not considered stable yet. For the latest stable version, please use Spring Framework 7.0.5! |
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 component classes or resource
locations, the configured ContextLoader determines how to load a context from default
configuration classes or a default location. In addition to component classes and
context resource locations, an application context can also be configured through
context customizers
or context initializers.
The following sections explain how to use @ContextConfiguration and related annotations
to configure a test ApplicationContext by using component classes (typically
@Configuration classes), XML configuration files, Groovy scripts, context customizers,
or context initializers. Alternatively, you can implement and configure your own custom
SmartContextLoader for advanced use cases.