The Spring Framework

org.springframework.test
Class AbstractSingleSpringContextTests

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
                  extended by org.springframework.test.AbstractSingleSpringContextTests
All Implemented Interfaces:
Test
Direct Known Subclasses:
AbstractDependencyInjectionSpringContextTests

public abstract class AbstractSingleSpringContextTests
extends AbstractSpringContextTests

Abstract JUnit 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.

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.

Since:
2.0
Author:
Juergen Hoeller, Rod Johnson
See Also:
getConfigLocations(), contextKey(), loadContext(Object)

Field Summary
protected  ConfigurableApplicationContext applicationContext
          Application context this test will run against
 
Fields inherited from class org.springframework.test.ConditionalTestCase
logger
 
Constructor Summary
AbstractSingleSpringContextTests()
          Default constructor for AbstractDependencyInjectionSpringContextTests.
AbstractSingleSpringContextTests(String name)
          Constructor for AbstractDependencyInjectionSpringContextTests with a JUnit name.
 
Method Summary
protected  Object contextKey()
          Return a key for this context.
protected  String[] getConfigLocations()
          Subclasses must implement this method to return the locations of their config files, unless they override contextKey() and loadContext(Object) instead.
 int getLoadCount()
          Return the current number of context load attempts.
protected  ConfigurableApplicationContext loadContext(Object key)
          This implementation assumes a key of type String array and loads a context from the given locations.
protected  ConfigurableApplicationContext loadContextLocations(String[] locations)
          Load an ApplicationContext from the given config locations.
protected  void onSetUp()
          Subclasses can override this method in place of the setUp() method, which is final in this class.
protected  void onTearDown()
          Subclasses can override this to add custom behavior on teardown.
protected  void prepareTestInstance()
          Prepare this test instance, for example populating its fields.
protected  void setDirty()
          Called to say that the "applicationContext" instance variable is dirty and should be reloaded.
protected  void setUp()
          This implementation is final.
protected  void tearDown()
          This implementation is final.
 
Methods inherited from class org.springframework.test.AbstractSpringContextTests
addContext, contextKeyString, getContext, hasCachedContext, setDirty
 
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, 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
 

Field Detail

applicationContext

protected ConfigurableApplicationContext applicationContext
Application context this test will run against

Constructor Detail

AbstractSingleSpringContextTests

public AbstractSingleSpringContextTests()
Default constructor for AbstractDependencyInjectionSpringContextTests.


AbstractSingleSpringContextTests

public AbstractSingleSpringContextTests(String name)
Constructor for AbstractDependencyInjectionSpringContextTests with a JUnit name.

Parameters:
name - the name of this text fixture
Method Detail

setUp

protected final void setUp()
                    throws Exception
This implementation is final. Override onSetUp for custom behavior.

Overrides:
setUp in class TestCase
Throws:
Exception
See Also:
onSetUp()

prepareTestInstance

protected void prepareTestInstance()
                            throws Exception
Prepare this test instance, for example populating its fields. The context has already been loaded at the time of this callback.

This implementation does nothing.

Throws:
Exception

onSetUp

protected void onSetUp()
                throws Exception
Subclasses can override this method in place of the setUp() method, which is final in this class. This implementation does nothing.

Throws:
Exception - simply let any exception propagate

setDirty

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


tearDown

protected final void tearDown()
                       throws Exception
This implementation is final. Override onTearDown for custom behavior.

Overrides:
tearDown in class TestCase
Throws:
Exception
See Also:
onTearDown()

onTearDown

protected void onTearDown()
                   throws Exception
Subclasses can override this to add custom behavior on teardown.

Throws:
Exception - simply let any exception propagate

contextKey

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.

See Also:
getConfigLocations()

loadContext

protected ConfigurableApplicationContext loadContext(Object key)
                                              throws Exception
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.

Specified by:
loadContext in class AbstractSpringContextTests
Parameters:
key - the context key
Returns:
the corresponding ApplicationContext instance (new)
Throws:
Exception
See Also:
getConfigLocations()

getConfigLocations

protected String[] getConfigLocations()
Subclasses must implement 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 returns an empty array.

Returns:
an array of config locations

loadContextLocations

protected ConfigurableApplicationContext loadContextLocations(String[] locations)
                                                       throws Exception
Load an ApplicationContext from the given config locations.

Parameters:
locations - the config locations
Returns:
the corresponding ApplicationContext instance (potentially cached)
Throws:
Exception

getLoadCount

public final int getLoadCount()
Return the current number of context load attempts.


The Spring Framework

Copyright © 2002-2006 The Spring Framework.