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 static Log logger
           
private  MergedContextConfiguration mergedContextConfiguration
           
private static long serialVersionUID
           
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
 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 merged context configuration.
 void markApplicationContextDirty()
          Call this method to signal that the application context associated with this test context is dirty and should be reloaded.
 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

logger

private static final Log logger

contextCache

private final ContextCache contextCache

mergedContextConfiguration

private final MergedContextConfiguration mergedContextConfiguration

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 concrete ContextLoader class is explicitly supplied via the @ContextConfiguration annotation, a DelegatingSmartContextLoader 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

loadApplicationContext

private ApplicationContext loadApplicationContext()
                                           throws java.lang.Exception
Load an ApplicationContext for this test context using the configured ContextLoader and merged context configuration. Supports both the SmartContextLoader and ContextLoader SPIs.

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

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