The Spring Framework

org.springframework.test
Class AbstractSpringContextTests

java.lang.Object
  extended by junit.framework.Assert
      extended by junit.framework.TestCase
          extended by org.springframework.test.ConditionalTestCase
              extended by org.springframework.test.AbstractSpringContextTests
All Implemented Interfaces:
Test
Direct Known Subclasses:
AbstractSingleSpringContextTests

public abstract class AbstractSpringContextTests
extends ConditionalTestCase

Superclass for JUnit test cases using Spring ApplicationContexts.

Maintains a static cache of contexts by key. This has significant performance benefit if initializing the context would take time. While initializing a Spring context itself is very quick, some beans in a context, such as a LocalSessionFactoryBean for working with Hibernate, may take some time to initialize. Hence it often makes sense to do that initializing once.

Any ApplicationContext created by this class will be asked to register a JVM shutdown hook for itself. Unless the context gets closed early, all context instances will be automatically closed on JVM shutdown. This allows for freeing external resources held by beans within the context, e.g. temporary files.

Normally you won't extend this class directly but rather extend one of its subclasses.

Since:
1.1.1
Author:
Rod Johnson, Juergen Hoeller
See Also:
AbstractDependencyInjectionSpringContextTests, AbstractTransactionalSpringContextTests, AbstractTransactionalDataSourceSpringContextTests

Field Summary
 
Fields inherited from class org.springframework.test.ConditionalTestCase
logger
 
Constructor Summary
AbstractSpringContextTests()
          Default constructor for AbstractSpringContextTests.
AbstractSpringContextTests(String name)
          Constructor for AbstractSpringContextTests with a JUnit name.
 
Method Summary
 void addContext(Object key, ConfigurableApplicationContext context)
          Explicitly add an ApplicationContext instance under a given key.
protected  String contextKeyString(Object contextKey)
          Subclasses can override this to return a String representation of their context key for use in logging.
protected  ConfigurableApplicationContext getContext(Object key)
          Obtain an ApplicationContext for the given key, potentially cached.
protected  boolean hasCachedContext(Object contextKey)
          Return whether there is a cached context for the given key.
protected abstract  ConfigurableApplicationContext loadContext(Object key)
          Load a new ApplicationContext for the given key.
protected  void setDirty(Object contextKey)
          Mark the context with the given key as dirty.
 
Methods inherited from class org.springframework.test.ConditionalTestCase
getDisabledTestCount, isDisabledInThisEnvironment, recordDisabled, runBare
 
Methods inherited from class junit.framework.TestCase
countTestCases, createResult, getName, run, run, runTest, setName, setUp, tearDown, toString
 
Methods inherited from class junit.framework.Assert
assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertFalse, assertFalse, assertNotNull, assertNotNull, assertNotSame, assertNotSame, assertNull, assertNull, assertSame, assertSame, assertTrue, assertTrue, fail, fail
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

AbstractSpringContextTests

public AbstractSpringContextTests()
Default constructor for AbstractSpringContextTests.


AbstractSpringContextTests

public AbstractSpringContextTests(String name)
Constructor for AbstractSpringContextTests with a JUnit name.

Method Detail

addContext

public final void addContext(Object key,
                             ConfigurableApplicationContext context)
Explicitly add an ApplicationContext instance under a given key.

This is not meant to be used by subclasses. It is rather exposed for special test suite environments.

Parameters:
key - the context key
context - the ApplicationContext instance

hasCachedContext

protected final boolean hasCachedContext(Object contextKey)
Return whether there is a cached context for the given key.

Parameters:
contextKey - the context key

getContext

protected final ConfigurableApplicationContext getContext(Object key)
                                                   throws Exception
Obtain an ApplicationContext for the given key, potentially cached.

Parameters:
key - the context key
Returns:
the corresponding ApplicationContext instance (potentially cached)
Throws:
Exception

setDirty

protected final void setDirty(Object contextKey)
Mark the context with the given key as dirty. This will cause the cached context to be reloaded before the next test case is executed.

Call this method only if you change the state of a singleton bean, potentially affecting future tests.


contextKeyString

protected String contextKeyString(Object contextKey)
Subclasses can override this to return a String representation of their context key for use in logging.

Parameters:
contextKey - the context key

loadContext

protected abstract ConfigurableApplicationContext loadContext(Object key)
                                                       throws Exception
Load a new ApplicationContext for the given key.

To be implemented by subclasses.

Parameters:
key - the context key
Returns:
the corresponding ApplicationContext instance (new)
Throws:
Exception

The Spring Framework

Copyright © 2002-2007 The Spring Framework.