public abstract class

AbstractSpringContextTests

extends ConditionalTestCase
java.lang.Object
   ↳ junit.framework.Assert
     ↳ junit.framework.TestCase
       ↳ org.springframework.test.ConditionalTestCase
         ↳ org.springframework.test.AbstractSpringContextTests
Known Direct Subclasses
Known Indirect Subclasses

This class is deprecated.
as of Spring 3.0, in favor of using the listener-based test context framework (AbstractJUnit38SpringContextTests)

Class Overview

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.

Summary

[Expand]
Inherited Fields
From class org.springframework.test.ConditionalTestCase
Public Constructors
AbstractSpringContextTests()
Default constructor for AbstractSpringContextTests.
AbstractSpringContextTests(String name)
Constructor for AbstractSpringContextTests with a JUnit name.
Public Methods
final void addContext(Object key, ConfigurableApplicationContext context)
Explicitly add an ApplicationContext instance under a given key.
Protected Methods
String contextKeyString(Object contextKey)
Subclasses can override this to return a String representation of their context key for use in caching and logging.
final ConfigurableApplicationContext getContext(Object key)
Obtain an ApplicationContext for the given key, potentially cached.
final boolean hasCachedContext(Object key)
Return whether there is a cached context for the given key.
boolean isContextKeyEmpty(Object key)
Determine if the supplied context key is empty.
abstract ConfigurableApplicationContext loadContext(Object key)
Load a new ApplicationContext for the given key.
final void setDirty(Object contextKey)
Mark the context with the given key as dirty.
[Expand]
Inherited Methods
From class org.springframework.test.ConditionalTestCase
From class junit.framework.TestCase
From class junit.framework.Assert
From class java.lang.Object
From interface junit.framework.Test

Public Constructors

public AbstractSpringContextTests ()

Default constructor for AbstractSpringContextTests.

public AbstractSpringContextTests (String name)

Constructor for AbstractSpringContextTests with a JUnit name.

Public Methods

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

Protected Methods

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

protected final ConfigurableApplicationContext getContext (Object key)

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

protected final boolean hasCachedContext (Object key)

Return whether there is a cached context for the given key.

Parameters
key the context key

protected boolean isContextKeyEmpty (Object key)

Determine 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

protected abstract ConfigurableApplicationContext loadContext (Object key)

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

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.