org.springframework.test.context
Class TestContextManager

java.lang.Object
  extended by org.springframework.test.context.TestContextManager

public class TestContextManager
extends java.lang.Object

TestContextManager is the main entry point into the Spring TestContext Framework, which provides support for loading and accessing application contexts, dependency injection of test instances, transactional execution of test methods, etc.

Specifically, a TestContextManager is responsible for managing a single TestContext and signaling events to all registered TestExecutionListeners at well defined test execution points:

Since:
2.5
Author:
Sam Brannen, Juergen Hoeller
See Also:
TestContext, TestExecutionListeners, ContextConfiguration, TransactionConfiguration

Field Summary
(package private) static ContextCache contextCache
          Cache of Spring application contexts.
private static java.lang.String[] DEFAULT_TEST_EXECUTION_LISTENER_CLASS_NAMES
           
private static Log logger
           
private  TestContext testContext
           
private  java.util.List<TestExecutionListener> testExecutionListeners
           
 
Constructor Summary
TestContextManager(java.lang.Class<?> testClass)
          Delegates to TestContextManager(Class, String) with a value of null for the default ContextLoader class name.
TestContextManager(java.lang.Class<?> testClass, java.lang.String defaultContextLoaderClassName)
          Constructs a new TestContextManager for the specified test class and automatically registers the TestExecutionListeners configured for the test class via the @TestExecutionListeners annotation.
 
Method Summary
 void afterTestClass()
          Hook for post-processing a test class after execution of all tests within the class.
 void afterTestMethod(java.lang.Object testInstance, java.lang.reflect.Method testMethod, java.lang.Throwable exception)
          Hook for post-processing a test after execution of the supplied test method, for example for tearing down test fixtures, ending a transaction, etc.
 void beforeTestClass()
          Hook for pre-processing a test class before execution of any tests within the class.
 void beforeTestMethod(java.lang.Object testInstance, java.lang.reflect.Method testMethod)
          Hook for pre-processing a test before execution of the supplied test method, for example for setting up test fixtures, starting a transaction, etc.
protected  java.util.Set<java.lang.Class<? extends TestExecutionListener>> getDefaultTestExecutionListenerClasses()
          Determine the default TestExecutionListener classes.
private  java.util.List<TestExecutionListener> getReversedTestExecutionListeners()
          Get a copy of the TestExecutionListeners registered for this TestContextManager in reverse order.
protected  TestContext getTestContext()
          Returns the TestContext managed by this TestContextManager.
 java.util.List<TestExecutionListener> getTestExecutionListeners()
          Get the current TestExecutionListeners registered for this TestContextManager.
 void prepareTestInstance(java.lang.Object testInstance)
          Hook for preparing a test instance prior to execution of any individual test methods, for example for injecting dependencies, etc.
 void registerTestExecutionListeners(TestExecutionListener... testExecutionListeners)
          Register the supplied TestExecutionListeners by appending them to the set of listeners used by this TestContextManager.
private  TestExecutionListener[] retrieveTestExecutionListeners(java.lang.Class<?> clazz)
          Retrieve an array of newly instantiated TestExecutionListeners for the specified class.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_TEST_EXECUTION_LISTENER_CLASS_NAMES

private static final java.lang.String[] DEFAULT_TEST_EXECUTION_LISTENER_CLASS_NAMES

logger

private static final Log logger

contextCache

static final ContextCache contextCache
Cache of Spring application contexts. This needs to be static, as tests may be destroyed and recreated between running individual test methods, for example with JUnit.


testContext

private final TestContext testContext

testExecutionListeners

private final java.util.List<TestExecutionListener> testExecutionListeners
Constructor Detail

TestContextManager

public TestContextManager(java.lang.Class<?> testClass)
Delegates to TestContextManager(Class, String) with a value of null for the default ContextLoader class name.


TestContextManager

public TestContextManager(java.lang.Class<?> testClass,
                          java.lang.String defaultContextLoaderClassName)
Constructs a new TestContextManager for the specified test class and automatically registers the TestExecutionListeners configured for the test class via the @TestExecutionListeners annotation.

Parameters:
testClass - the test class to be managed
defaultContextLoaderClassName - the name of the default ContextLoader class to use (may be null)
See Also:
registerTestExecutionListeners(TestExecutionListener...), retrieveTestExecutionListeners(Class)
Method Detail

getTestContext

protected final TestContext getTestContext()
Returns the TestContext managed by this TestContextManager.


registerTestExecutionListeners

public void registerTestExecutionListeners(TestExecutionListener... testExecutionListeners)
Register the supplied TestExecutionListeners by appending them to the set of listeners used by this TestContextManager.


getTestExecutionListeners

public final java.util.List<TestExecutionListener> getTestExecutionListeners()
Get the current TestExecutionListeners registered for this TestContextManager.

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.


getReversedTestExecutionListeners

private java.util.List<TestExecutionListener> getReversedTestExecutionListeners()
Get a copy of the TestExecutionListeners registered for this TestContextManager in reverse order.


retrieveTestExecutionListeners

private TestExecutionListener[] retrieveTestExecutionListeners(java.lang.Class<?> clazz)
Retrieve an array of newly instantiated TestExecutionListeners for the specified class. If @TestExecutionListeners is not present on the supplied class, the default listeners will be returned.

Note that the inheritListeners flag of @TestExecutionListeners will be taken into consideration. Specifically, if the inheritListeners flag is set to true, listeners defined in the annotated class will be appended to the listeners defined in superclasses.

Parameters:
clazz - the test class for which the listeners should be retrieved
Returns:
an array of TestExecutionListeners for the specified class

getDefaultTestExecutionListenerClasses

protected java.util.Set<java.lang.Class<? extends TestExecutionListener>> getDefaultTestExecutionListenerClasses()
Determine the default TestExecutionListener classes.


beforeTestClass

public void beforeTestClass()
                     throws java.lang.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 TestExecutionListener a chance to pre-process the test class execution. If a listener throws an exception, however, the remaining registered listeners will not be called.

Throws:
java.lang.Exception - if a registered TestExecutionListener throws an exception
See Also:
getTestExecutionListeners()

prepareTestInstance

public void prepareTestInstance(java.lang.Object testInstance)
                         throws java.lang.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 TestContext will be updated with the supplied testInstance.

An attempt will be made to give each registered TestExecutionListener a 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 (never null)
Throws:
java.lang.Exception - if a registered TestExecutionListener throws an exception
See Also:
getTestExecutionListeners()

beforeTestMethod

public void beforeTestMethod(java.lang.Object testInstance,
                             java.lang.reflect.Method testMethod)
                      throws java.lang.Exception
Hook for pre-processing a test before execution of the supplied test 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 TestContext will be updated with the supplied testInstance and testMethod.

An attempt will be made to give each registered TestExecutionListener a 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 (never null)
testMethod - the test method which is about to be executed on the test instance
Throws:
java.lang.Exception - if a registered TestExecutionListener throws an exception
See Also:
getTestExecutionListeners()

afterTestMethod

public void afterTestMethod(java.lang.Object testInstance,
                            java.lang.reflect.Method testMethod,
                            java.lang.Throwable exception)
                     throws java.lang.Exception
Hook for post-processing a test after execution of the supplied test 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 TestContext will be updated with the supplied testInstance, testMethod, and exception.

Each registered TestExecutionListener will 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 (never null)
testMethod - the test method which has just been executed on the test instance
exception - the exception that was thrown during execution of the test method or by a TestExecutionListener, or null if none was thrown
Throws:
java.lang.Exception - if a registered TestExecutionListener throws an exception
See Also:
getTestExecutionListeners()

afterTestClass

public void afterTestClass()
                    throws java.lang.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 TestExecutionListener will 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:
java.lang.Exception - if a registered TestExecutionListener throws an exception
See Also:
getTestExecutionListeners()