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:
junit.framework.Test
Direct Known Subclasses:
AbstractSingleSpringContextTests

public abstract class AbstractSpringContextTests
extends ConditionalTestCase

Superclass for JUnit 3.8 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 one of its subclasses.

Since:
1.1.1
Author:
Rod Johnson, Juergen Hoeller, Sam Brannen
See Also:
AbstractSingleSpringContextTests, 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 caching and logging.
protected  ConfigurableApplicationContext getContext(Object key)
          Obtain an ApplicationContext for the given key, potentially cached.
protected  boolean hasCachedContext(Object key)
          Return whether there is a cached context for the given key.
protected  boolean isContextKeyEmpty(Object key)
           Determines if the supplied context key is empty.
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, failNotEquals, failNotSame, failSame, format
 
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 key)
Return whether there is a cached context for the given key.

Parameters:
key - the context key

isContextKeyEmpty

protected boolean isContextKeyEmpty(Object key)

Determines if the supplied context key is empty.

By default, null values, empty strings, and zero-length arrays are considered empty.

Parameters:
key - the context key to check
Returns:
true if the supplied context key is empty.

getContext

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

Parameters:
key - the context key; may be null.
Returns:
the corresponding ApplicationContext instance (potentially cached), or null if the provided key is empty.
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 caching and 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.