org.springframework.test.context
Class TestContext

java.lang.Object
  extended by org.springframework.core.AttributeAccessorSupport
      extended by org.springframework.test.context.TestContext
All Implemented Interfaces:
java.io.Serializable, AttributeAccessor

public class TestContext
extends AttributeAccessorSupport

TestContext encapsulates the context in which a test is executed, agnostic of the actual testing framework in use.

Since:
2.5
Author:
Sam Brannen, Juergen Hoeller
See Also:
Serialized Form

Field Summary
private  ContextCache contextCache
           
private  ContextLoader contextLoader
           
private  java.lang.String[] locations
           
private static Log logger
           
private static long serialVersionUID
           
private static java.lang.String STANDARD_DEFAULT_CONTEXT_LOADER_CLASS_NAME
           
private  java.lang.Class<?> testClass
           
private  java.lang.Throwable testException
           
private  java.lang.Object testInstance
           
private  java.lang.reflect.Method testMethod
           
 
Constructor Summary
TestContext(java.lang.Class<?> testClass, ContextCache contextCache)
          Delegates to TestContext(Class, ContextCache, String) with a value of null for the default ContextLoader class name.
TestContext(java.lang.Class<?> testClass, ContextCache contextCache, java.lang.String defaultContextLoaderClassName)
          Construct a new test context for the supplied test class and context cache and parse the corresponding @ContextConfiguration annotation, if present.
 
Method Summary
private  java.lang.String contextKeyString(java.io.Serializable key)
          Convert the supplied context key to a String representation for use in caching, logging, etc.
 ApplicationContext getApplicationContext()
          Get the application context for this test context, possibly cached.
 java.lang.Class<?> getTestClass()
          Get the test class for this test context.
 java.lang.Throwable getTestException()
          Get the exception that was thrown during execution of the test method.
 java.lang.Object getTestInstance()
          Get the current test instance for this test context.
 java.lang.reflect.Method getTestMethod()
          Get the current test method for this test context.
private  ApplicationContext loadApplicationContext()
          Load an ApplicationContext for this test context using the configured ContextLoader and resource locations.
 void markApplicationContextDirty()
          Call this method to signal that the application context associated with this test context is dirty and should be reloaded.
private  java.lang.Class<? extends ContextLoader> retrieveContextLoaderClass(java.lang.Class<?> clazz, java.lang.String defaultContextLoaderClassName)
           Retrieve the ContextLoader Class to use for the supplied test class.
private  java.lang.String[] retrieveContextLocations(ContextLoader contextLoader, java.lang.Class<?> clazz)
          Retrieve ApplicationContext resource locations for the supplied class, using the supplied ContextLoader to process the locations.
 java.lang.String toString()
          Provide a String representation of this test context's state.
(package private)  void updateState(java.lang.Object testInstance, java.lang.reflect.Method testMethod, java.lang.Throwable testException)
          Update this test context to reflect the state of the currently executing test.
 
Methods inherited from class org.springframework.core.AttributeAccessorSupport
attributeNames, copyAttributesFrom, equals, getAttribute, hasAttribute, hashCode, removeAttribute, setAttribute
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

serialVersionUID

private static final long serialVersionUID
See Also:
Constant Field Values

STANDARD_DEFAULT_CONTEXT_LOADER_CLASS_NAME

private static final java.lang.String STANDARD_DEFAULT_CONTEXT_LOADER_CLASS_NAME
See Also:
Constant Field Values

logger

private static final Log logger

contextCache

private final ContextCache contextCache

contextLoader

private final ContextLoader contextLoader

locations

private final java.lang.String[] locations

testClass

private final java.lang.Class<?> testClass

testInstance

private java.lang.Object testInstance

testMethod

private java.lang.reflect.Method testMethod

testException

private java.lang.Throwable testException
Constructor Detail

TestContext

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


TestContext

TestContext(java.lang.Class<?> testClass,
            ContextCache contextCache,
            java.lang.String defaultContextLoaderClassName)
Construct a new test context for the supplied test class and context cache and parse the corresponding @ContextConfiguration annotation, if present.

If the supplied class name for the default ContextLoader is null or empty and no ContextLoader class is explicitly supplied via the @ContextConfiguration annotation, a GenericXmlContextLoader will be used instead.

Parameters:
testClass - the test class for which the test context should be constructed (must not be null)
contextCache - the context cache from which the constructed test context should retrieve application contexts (must not be null)
defaultContextLoaderClassName - the name of the default ContextLoader class to use (may be null)
Method Detail

retrieveContextLoaderClass

private java.lang.Class<? extends ContextLoader> retrieveContextLoaderClass(java.lang.Class<?> clazz,
                                                                            java.lang.String defaultContextLoaderClassName)

Retrieve the ContextLoader Class to use for the supplied test class.

  1. If the loader attribute of @ContextConfiguration is configured with an explicit class, that class will be returned.
  2. If a loader class is not specified, the class hierarchy will be traversed to find a parent class annotated with @ContextConfiguration; go to step #1.
  3. If no explicit loader class is found after traversing the class hierarchy, an attempt will be made to load and return the class with the supplied defaultContextLoaderClassName.

Parameters:
clazz - the class for which to retrieve ContextLoader class; must not be null
defaultContextLoaderClassName - the name of the default ContextLoader class to use; must not be null or empty
Returns:
the ContextLoader class to use for the specified class
Throws:
java.lang.IllegalArgumentException - if @ContextConfiguration is not present on the supplied class

retrieveContextLocations

private java.lang.String[] retrieveContextLocations(ContextLoader contextLoader,
                                                    java.lang.Class<?> clazz)
Retrieve ApplicationContext resource locations for the supplied class, using the supplied ContextLoader to process the locations.

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

Parameters:
contextLoader - the ContextLoader to use for processing the locations (must not be null)
clazz - the class for which to retrieve the resource locations (must not be null)
Returns:
the list of ApplicationContext resource locations for the specified class, including locations from superclasses if appropriate
Throws:
java.lang.IllegalArgumentException - if @ContextConfiguration is not present on the supplied class

loadApplicationContext

private ApplicationContext loadApplicationContext()
                                           throws java.lang.Exception
Load an ApplicationContext for this test context using the configured ContextLoader and resource locations.

Throws:
java.lang.Exception - if an error occurs while loading the application context

contextKeyString

private java.lang.String contextKeyString(java.io.Serializable key)
Convert the supplied context key to a String representation for use in caching, logging, etc.


getApplicationContext

public ApplicationContext getApplicationContext()
Get the application context for this test context, possibly cached.

Returns:
the application context
Throws:
java.lang.IllegalStateException - if an error occurs while retrieving the application context

getTestClass

public final java.lang.Class<?> getTestClass()
Get the test class for this test context.

Returns:
the test class (never null)

getTestInstance

public final java.lang.Object getTestInstance()
Get the current test instance for this test context.

Note: this is a mutable property.

Returns:
the current test instance (may be null)
See Also:
updateState(Object,Method,Throwable)

getTestMethod

public final java.lang.reflect.Method getTestMethod()
Get the current test method for this test context.

Note: this is a mutable property.

Returns:
the current test method (may be null)
See Also:
updateState(Object, Method, Throwable)

getTestException

public final java.lang.Throwable getTestException()
Get the exception that was thrown during execution of the test method.

Note: this is a mutable property.

Returns:
the exception that was thrown, or null if no exception was thrown
See Also:
updateState(Object, Method, Throwable)

markApplicationContextDirty

public void markApplicationContextDirty()
Call this method to signal that the application context associated with this test context is dirty and should be reloaded. Do this if a test has modified the context (for example, by replacing a bean definition).


updateState

void updateState(java.lang.Object testInstance,
                 java.lang.reflect.Method testMethod,
                 java.lang.Throwable testException)
Update this test context to reflect the state of the currently executing test.

Parameters:
testInstance - the current test instance (may be null)
testMethod - the current test method (may be null)
testException - the exception that was thrown in the test method, or null if no exception was thrown

toString

public java.lang.String toString()
Provide a String representation of this test context's state.

Overrides:
toString in class java.lang.Object