Class TestContextManager
- java.lang.Object
- org.springframework.test.context.TestContextManager
public class TestContextManager extends Object
TestContextManageris the main entry point into the Spring TestContext Framework.Specifically, a
TestContextManageris responsible for managing a singleTestContextand signaling events to all registeredTestExecutionListenersat the following test execution points:before test class execution: prior to any before class callbacks of a particular testing framework (e.g., JUnit 4's@BeforeClass)test instance preparation: immediately following instantiation of the test instancebefore test method execution: prior to any before method callbacks of a particular testing framework (e.g., JUnit 4's@Before)after test method execution: after any after method callbacks of a particular testing framework (e.g., JUnit 4's@After)after test class execution: after any after class callbacks of a particular testing framework (e.g., JUnit 4's@AfterClass)
Support for loading and accessing
application contexts, dependency injection of test instances,transactionalexecution of test methods, etc. is provided byContextLoadersandTestExecutionListeners, which are configured via@ContextConfigurationand@TestExecutionListeners.Bootstrapping of the
TestContext, the defaultContextLoader, defaultTestExecutionListeners, and their collaborators is performed by aTestContextBootstrapper, which is configured via@BootstrapWith.- Since:
- 2.5
- Author:
- Sam Brannen, Juergen Hoeller
- See Also:
BootstrapWith,BootstrapContext,TestContextBootstrapper,TestContext,TestExecutionListener,TestExecutionListeners,ContextConfiguration,ContextHierarchy
Constructor Summary
Constructors Constructor Description TestContextManager(Class<?> testClass)Construct a newTestContextManagerfor the supplied test class.TestContextManager(TestContextBootstrapper testContextBootstrapper)Construct a newTestContextManagerusing the suppliedTestContextBootstrapperand register the necessaryTestExecutionListeners.
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidafterTestClass()Hook for post-processing a test class after execution of all tests within the class.voidafterTestMethod(Object testInstance, Method testMethod, Throwable exception)Hook for post-processing a test after execution of the suppliedtest method, for example for tearing down test fixtures, ending a transaction, etc.voidbeforeTestClass()Hook for pre-processing a test class before execution of any tests within the class.voidbeforeTestMethod(Object testInstance, Method testMethod)Hook for pre-processing a test before execution of the suppliedtest method, for example for setting up test fixtures, starting a transaction, etc.TestContextgetTestContext()Get theTestContextmanaged by thisTestContextManager.List<TestExecutionListener>getTestExecutionListeners()Get the currentTestExecutionListenersregistered for thisTestContextManager.voidprepareTestInstance(Object testInstance)Hook for preparing a test instance prior to execution of any individual test methods, for example for injecting dependencies, etc.voidregisterTestExecutionListeners(List<TestExecutionListener> testExecutionListeners)Register the supplied list ofTestExecutionListenersby appending them to the list of listeners used by thisTestContextManager.voidregisterTestExecutionListeners(TestExecutionListener... testExecutionListeners)Register the supplied array ofTestExecutionListenersby appending them to the list of listeners used by thisTestContextManager.
Constructor Detail
TestContextManager
public TestContextManager(Class<?> testClass)
Construct a newTestContextManagerfor the supplied test class.Delegates to
TestContextManager(TestContextBootstrapper)with theTestContextBootstrapperconfigured for the test class. If the@BootstrapWithannotation is present on the test class, either directly or as a meta-annotation, then itsvaluewill be used as the bootstrapper type; otherwise, theDefaultTestContextBootstrapperwill be used.- Parameters:
testClass- the test class to be managed- See Also:
TestContextManager(TestContextBootstrapper)
TestContextManager
public TestContextManager(TestContextBootstrapper testContextBootstrapper)
Construct a newTestContextManagerusing the suppliedTestContextBootstrapperand register the necessaryTestExecutionListeners.Delegates to the supplied
TestContextBootstrapperfor building theTestContextand retrieving theTestExecutionListeners.- Parameters:
testContextBootstrapper- the bootstrapper to use- See Also:
TestContextBootstrapper.buildTestContext(),TestContextBootstrapper.getTestExecutionListeners(),registerTestExecutionListeners(java.util.List<org.springframework.test.context.TestExecutionListener>)
Method Detail
getTestContext
public final TestContext getTestContext()
Get theTestContextmanaged by thisTestContextManager.
registerTestExecutionListeners
public void registerTestExecutionListeners(List<TestExecutionListener> testExecutionListeners)
Register the supplied list ofTestExecutionListenersby appending them to the list of listeners used by thisTestContextManager.
registerTestExecutionListeners
public void registerTestExecutionListeners(TestExecutionListener... testExecutionListeners)
Register the supplied array ofTestExecutionListenersby appending them to the list of listeners used by thisTestContextManager.
getTestExecutionListeners
public final List<TestExecutionListener> getTestExecutionListeners()
Get the currentTestExecutionListenersregistered for thisTestContextManager.Allows for modifications, e.g. adding a listener to the beginning of the list. However, make sure to keep the list stable while actually executing tests.
beforeTestClass
public void beforeTestClass() throws Exception
Hook for pre-processing a test class before execution of any tests within the class. Should be called prior to any framework-specific before class methods (e.g., methods annotated with JUnit's@BeforeClass).An attempt will be made to give each registered
TestExecutionListenera chance to pre-process the test class execution. If a listener throws an exception, however, the remaining registered listeners will not be called.- Throws:
Exception- if a registered TestExecutionListener throws an exception- See Also:
getTestExecutionListeners()
prepareTestInstance
public void prepareTestInstance(Object testInstance) throws Exception
Hook for preparing a test instance prior to execution of any individual test methods, for example for injecting dependencies, etc. Should be called immediately after instantiation of the test instance.The managed
TestContextwill be updated with the suppliedtestInstance.An attempt will be made to give each registered
TestExecutionListenera chance to prepare the test instance. If a listener throws an exception, however, the remaining registered listeners will not be called.- Parameters:
testInstance- the test instance to prepare (nevernull)- Throws:
Exception- if a registered TestExecutionListener throws an exception- See Also:
getTestExecutionListeners()
beforeTestMethod
public void beforeTestMethod(Object testInstance, Method testMethod) throws Exception
Hook for pre-processing a test before execution of the suppliedtest method, for example for setting up test fixtures, starting a transaction, etc. Should be called prior to any framework-specific before methods (e.g., methods annotated with JUnit's@Before).The managed
TestContextwill be updated with the suppliedtestInstanceandtestMethod.An attempt will be made to give each registered
TestExecutionListenera chance to pre-process the test method execution. If a listener throws an exception, however, the remaining registered listeners will not be called.- Parameters:
testInstance- the current test instance (nevernull)testMethod- the test method which is about to be executed on the test instance- Throws:
Exception- if a registered TestExecutionListener throws an exception- See Also:
getTestExecutionListeners()
afterTestMethod
public void afterTestMethod(Object testInstance, Method testMethod, Throwable exception) throws Exception
Hook for post-processing a test after execution of the suppliedtest method, for example for tearing down test fixtures, ending a transaction, etc. Should be called after any framework-specific after methods (e.g., methods annotated with JUnit's@After).The managed
TestContextwill be updated with the suppliedtestInstance,testMethod, andexception.Each registered
TestExecutionListenerwill be given a chance to post-process the test method execution. If a listener throws an exception, the remaining registered listeners will still be called, but the first exception thrown will be tracked and rethrown after all listeners have executed. Note that registered listeners will be executed in the opposite order in which they were registered.- Parameters:
testInstance- the current test instance (nevernull)testMethod- the test method which has just been executed on the test instanceexception- the exception that was thrown during execution of the test method or by a TestExecutionListener, ornullif none was thrown- Throws:
Exception- if a registered TestExecutionListener throws an exception- See Also:
getTestExecutionListeners()
afterTestClass
public void afterTestClass() throws Exception
Hook for post-processing a test class after execution of all tests within the class. Should be called after any framework-specific after class methods (e.g., methods annotated with JUnit's@AfterClass).Each registered
TestExecutionListenerwill be given a chance to post-process the test class. If a listener throws an exception, the remaining registered listeners will still be called, but the first exception thrown will be tracked and rethrown after all listeners have executed. Note that registered listeners will be executed in the opposite order in which they were registered.- Throws:
Exception- if a registered TestExecutionListener throws an exception- See Also:
getTestExecutionListeners()