The Spring Framework

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:
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

Constructor Summary
TestContext(Class<?> testClass, org.springframework.test.context.ContextCache<String,ApplicationContext> contextCache)
          Constructs a new test context for the supplied test class and context cache and parses the corresponding @ContextConfiguration annotation, if present.
 
Method Summary
 ApplicationContext getApplicationContext()
           Gets the application context for this test context, possibly cached.
 Class<?> getTestClass()
           Gets the test class for this test context.
 Throwable getTestException()
           Gets the exception that was thrown during execution of the test method.
 Object getTestInstance()
           Gets the current test instance for this test context.
 Method getTestMethod()
           Gets the current test method for this test context.
 void markApplicationContextDirty()
          Call this method to signal that the application context associated with this test context is dirty and should be reloaded.
 String toString()
          Provides a string representation of this test context's test class, application context resource locations, test instance, test method, and test exception.
 
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
 

Constructor Detail

TestContext

public TestContext(Class<?> testClass,
                   org.springframework.test.context.ContextCache<String,ApplicationContext> contextCache)
Constructs a new test context for the supplied test class and context cache and parses the corresponding @ContextConfiguration annotation, if present.

Parameters:
testClass - the Class object corresponding to 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.
Method Detail

getApplicationContext

public ApplicationContext getApplicationContext()
                                         throws Exception

Gets the application context for this test context, possibly cached.

Returns:
the application context; may be null if the current test context is not configured to use an application context.
Throws:
Exception - if an error occurs while retrieving the application context.

getTestClass

public final Class<?> getTestClass()

Gets the test class for this test context.

Returns:
the test class, never null.

getTestInstance

public final Object getTestInstance()

Gets 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 Method getTestMethod()

Gets 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 Throwable getTestException()

Gets 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).


toString

public String toString()
Provides a string representation of this test context's test class, application context resource locations, test instance, test method, and test exception.

Overrides:
toString in class Object

The Spring Framework

Copyright © 2002-2007 The Spring Framework.