For the latest stable version, please use Spring Framework 6.2.0!spring-doc.cn

Key Abstractions

The core of the framework consists of the TestContextManager class and the TestContext, TestExecutionListener, and SmartContextLoader interfaces. A TestContextManager is created for each test class (for example, for the execution of all test methods within a single test class in JUnit Jupiter). The TestContextManager, in turn, manages a TestContext that holds the context of the current test. The TestContextManager also updates the state of the TestContext as the test progresses and delegates to TestExecutionListener implementations, which instrument the actual test execution by providing dependency injection, managing transactions, and so on. A SmartContextLoader is responsible for loading an ApplicationContext for a given test class. See the javadoc and the Spring test suite for further information and examples of various implementations.spring-doc.cn

TestContext

TestContext encapsulates the context in which a test is run (agnostic of the actual testing framework in use) and provides context management and caching support for the test instance for which it is responsible. The TestContext also delegates to a SmartContextLoader to load an ApplicationContext if requested.spring-doc.cn

TestContextManager

TestContextManager is the main entry point into the Spring TestContext Framework and is responsible for managing a single TestContext and signaling events to each registered TestExecutionListener at well-defined test execution points:spring-doc.cn

  • Prior to any “before class” or “before all” methods of a particular testing framework.spring-doc.cn

  • Test instance post-processing.spring-doc.cn

  • Prior to any “before” or “before each” methods of a particular testing framework.spring-doc.cn

  • Immediately before execution of the test method but after test setup.spring-doc.cn

  • Immediately after execution of the test method but before test tear down.spring-doc.cn

  • After any “after” or “after each” methods of a particular testing framework.spring-doc.cn

  • After any “after class” or “after all” methods of a particular testing framework.spring-doc.cn

TestExecutionListener

TestExecutionListener defines the API for reacting to test-execution events published by the TestContextManager with which the listener is registered. See TestExecutionListener Configuration.spring-doc.cn

Context Loaders

ContextLoader is a strategy interface for loading an ApplicationContext for an integration test managed by the Spring TestContext Framework. You should implement SmartContextLoader instead of this interface to provide support for component classes, active bean definition profiles, test property sources, context hierarchies, and WebApplicationContext support.spring-doc.cn

SmartContextLoader is an extension of the ContextLoader interface that supersedes the original minimal ContextLoader SPI. Specifically, a SmartContextLoader can choose to process resource locations, component classes, or context initializers. Furthermore, a SmartContextLoader can set active bean definition profiles and test property sources in the context that it loads.spring-doc.cn

Spring provides the following implementations:spring-doc.cn

  • DelegatingSmartContextLoader: One of two default loaders, it delegates internally to an AnnotationConfigContextLoader, a GenericXmlContextLoader, or a GenericGroovyXmlContextLoader, depending either on the configuration declared for the test class or on the presence of default locations or default configuration classes. Groovy support is enabled only if Groovy is on the classpath.spring-doc.cn

  • WebDelegatingSmartContextLoader: One of two default loaders, it delegates internally to an AnnotationConfigWebContextLoader, a GenericXmlWebContextLoader, or a GenericGroovyXmlWebContextLoader, depending either on the configuration declared for the test class or on the presence of default locations or default configuration classes. A web ContextLoader is used only if @WebAppConfiguration is present on the test class. Groovy support is enabled only if Groovy is on the classpath.spring-doc.cn

  • AnnotationConfigContextLoader: Loads a standard ApplicationContext from component classes.spring-doc.cn

  • AnnotationConfigWebContextLoader: Loads a WebApplicationContext from component classes.spring-doc.cn

  • GenericGroovyXmlContextLoader: Loads a standard ApplicationContext from resource locations that are either Groovy scripts or XML configuration files.spring-doc.cn

  • GenericGroovyXmlWebContextLoader: Loads a WebApplicationContext from resource locations that are either Groovy scripts or XML configuration files.spring-doc.cn

  • GenericXmlContextLoader: Loads a standard ApplicationContext from XML resource locations.spring-doc.cn

  • GenericXmlWebContextLoader: Loads a WebApplicationContext from XML resource locations.spring-doc.cn