public abstract class

AbstractSingleSpringContextTests

extends AbstractSpringContextTests
java.lang.Object
   ↳ junit.framework.Assert
     ↳ junit.framework.TestCase
       ↳ org.springframework.test.ConditionalTestCase
         ↳ org.springframework.test.AbstractSpringContextTests
           ↳ org.springframework.test.AbstractSingleSpringContextTests
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

Abstract JUnit 3.8 test class that holds and exposes a single Spring ApplicationContext.

This class will cache contexts based on a context key: normally the config locations String array describing the Spring resource descriptors making up the context. Unless the setDirty() method is called by a test, the context will not be reloaded, even across different subclasses of this test. This is particularly beneficial if your context is slow to construct, for example if you are using Hibernate and the time taken to load the mappings is an issue.

For such standard usage, simply override the getConfigLocations() method and provide the desired config files. For alternative configuration options, see getConfigPath() and getConfigPaths().

If you don't want to load a standard context from an array of config locations, you can override the contextKey() method. In conjunction with this you typically need to override the loadContext(Object) method, which by default loads the locations specified in the getConfigLocations() method.

WARNING: When doing integration tests from within Eclipse, only use classpath resource URLs. Else, you may see misleading failures when changing context locations.

Summary

Fields
protected ConfigurableApplicationContext applicationContext Application context this test will run against
[Expand]
Inherited Fields
From class org.springframework.test.ConditionalTestCase
Public Constructors
AbstractSingleSpringContextTests()
Default constructor for AbstractSingleSpringContextTests.
AbstractSingleSpringContextTests(String name)
Constructor for AbstractSingleSpringContextTests with a JUnit name.
Public Methods
final ConfigurableApplicationContext getApplicationContext()
Return the ApplicationContext that this base class manages; may be null.
final int getLoadCount()
Return the current number of context load attempts.
Protected Methods
Object contextKey()
Return a key for this context.
ConfigurableApplicationContext createApplicationContext(String[] locations)
Create a Spring ConfigurableApplicationContext for use by this test.
BeanDefinitionReader createBeanDefinitionReader(GenericApplicationContext context)
Factory method for creating new BeanDefinitionReaders for loading bean definitions into the supplied context.
void customizeBeanFactory(DefaultListableBeanFactory beanFactory)
Customize the internal bean factory of the ApplicationContext used by this test.
String[] getConfigLocations()
Subclasses can override this method to return the locations of their config files, unless they override contextKey() and loadContext(Object) instead.
String getConfigPath()
Subclasses can override this method to return a single path to a config file, relative to the concrete test class.
String[] getConfigPaths()
Subclasses can override this method to return paths to their config files, relative to the concrete test class.
ConfigurableApplicationContext loadContext(Object key)
This implementation assumes a key of type String array and loads a context from the given locations.
ConfigurableApplicationContext loadContextLocations(String[] locations)
Load a Spring ApplicationContext from the given config locations.
void onSetUp()
Subclasses can override this method in place of the setUp() method, which is final in this class.
void onTearDown()
Subclasses can override this to add custom behavior on teardown.
void prepareApplicationContext(GenericApplicationContext context)
Prepare the GenericApplicationContext used by this test.
void prepareTestInstance()
Prepare this test instance, for example populating its fields.
void setDirty()
Called to say that the "applicationContext" instance variable is dirty and should be reloaded.
final void setUp()
This implementation is final.
final void tearDown()
This implementation is final.
[Expand]
Inherited Methods
From class org.springframework.test.AbstractSpringContextTests
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

Fields

protected ConfigurableApplicationContext applicationContext

Application context this test will run against

Public Constructors

public AbstractSingleSpringContextTests ()

Default constructor for AbstractSingleSpringContextTests.

public AbstractSingleSpringContextTests (String name)

Constructor for AbstractSingleSpringContextTests with a JUnit name.

Parameters
name the name of this text fixture

Public Methods

public final ConfigurableApplicationContext getApplicationContext ()

Return the ApplicationContext that this base class manages; may be null.

public final int getLoadCount ()

Return the current number of context load attempts.

Protected Methods

protected Object contextKey ()

Return a key for this context. Default is the config location array as determined by getConfigLocations().

If you override this method, you will typically have to override loadContext(Object) as well, being able to handle the key type that this method returns.

Returns
  • the context key

protected ConfigurableApplicationContext createApplicationContext (String[] locations)

Create a Spring ConfigurableApplicationContext for use by this test.

The default implementation creates a standard GenericApplicationContext instance, calls the prepareApplicationContext(GenericApplicationContext) prepareApplicationContext} method and the customizeBeanFactory method to allow for customizing the context and its DefaultListableBeanFactory, populates the context from the specified config locations through the configured BeanDefinitionReader, and finally refreshes the context.

Parameters
locations the config locations (as Spring resource locations, e.g. full classpath locations or any kind of URL)
Returns
  • the GenericApplicationContext instance

protected BeanDefinitionReader createBeanDefinitionReader (GenericApplicationContext context)

Factory method for creating new BeanDefinitionReaders for loading bean definitions into the supplied context.

The default implementation creates a new XmlBeanDefinitionReader. Can be overridden in subclasses to provide a different BeanDefinitionReader implementation.

Parameters
context the context for which the BeanDefinitionReader should be created
Returns
  • a BeanDefinitionReader for the supplied context

protected void customizeBeanFactory (DefaultListableBeanFactory beanFactory)

Customize the internal bean factory of the ApplicationContext used by this test. Called before bean definitions are read.

The default implementation is empty. Can be overridden in subclasses to customize DefaultListableBeanFactory's standard settings.

Parameters
beanFactory the newly created bean factory for this context

protected String[] getConfigLocations ()

Subclasses can override this method to return the locations of their config files, unless they override contextKey() and loadContext(Object) instead.

A plain path will be treated as class path location, e.g.: "org/springframework/whatever/foo.xml". Note however that you may prefix path locations with standard Spring resource prefixes. Therefore, a config location path prefixed with "classpath:" with behave the same as a plain path, but a config location such as "file:/some/path/path/location/appContext.xml" will be treated as a filesystem location.

The default implementation builds config locations for the config paths specified through getConfigPaths().

Returns
  • an array of config locations

protected String getConfigPath ()

Subclasses can override this method to return a single path to a config file, relative to the concrete test class.

A plain path, e.g. "context.xml", will be loaded as classpath resource from the same package that the concrete test class is defined in. A path starting with a slash is treated as fully qualified class path location, e.g.: "/org/springframework/whatever/foo.xml".

The default implementation simply returns null.

Returns
  • an array of config locations

protected String[] getConfigPaths ()

Subclasses can override this method to return paths to their config files, relative to the concrete test class.

A plain path, e.g. "context.xml", will be loaded as classpath resource from the same package that the concrete test class is defined in. A path starting with a slash is treated as fully qualified class path location, e.g.: "/org/springframework/whatever/foo.xml".

The default implementation builds an array for the config path specified through getConfigPath().

Returns
  • an array of config locations

protected ConfigurableApplicationContext loadContext (Object key)

This implementation assumes a key of type String array and loads a context from the given locations.

If you override contextKey(), you will typically have to override this method as well, being able to handle the key type that contextKey() returns.

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

protected ConfigurableApplicationContext loadContextLocations (String[] locations)

Load a Spring ApplicationContext from the given config locations.

The default implementation creates a standard GenericApplicationContext, allowing for customizing the internal bean factory through customizeBeanFactory(DefaultListableBeanFactory).

Parameters
locations the config locations (as Spring resource locations, e.g. full classpath locations or any kind of URL)
Returns
  • the corresponding ApplicationContext instance (potentially cached)
Throws
Exception if context loading failed

protected void onSetUp ()

Subclasses can override this method in place of the setUp() method, which is final in this class.

The default implementation does nothing.

Throws
Exception simply let any exception propagate

protected void onTearDown ()

Subclasses can override this to add custom behavior on teardown.

Throws
Exception simply let any exception propagate

protected void prepareApplicationContext (GenericApplicationContext context)

Prepare the GenericApplicationContext used by this test. Called before bean definitions are read.

The default implementation is empty. Can be overridden in subclasses to customize GenericApplicationContext's standard settings.

Parameters
context the context for which the BeanDefinitionReader should be created

protected void prepareTestInstance ()

Prepare this test instance, for example populating its fields. The context has already been loaded at the time of this callback.

The default implementation does nothing.

Throws
Exception in case of preparation failure

protected void setDirty ()

Called to say that the "applicationContext" instance variable is dirty and should be reloaded. We need to do this if a test has modified the context (for example, by replacing a bean definition).

protected final void setUp ()

This implementation is final. Override onSetUp for custom behavior.

Throws
Exception
See Also

protected final void tearDown ()

This implementation is final. Override onTearDown for custom behavior.

Throws
Exception
See Also