org.springframework.test
Class AbstractDependencyInjectionSpringContextTests

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

public abstract class AbstractDependencyInjectionSpringContextTests
extends AbstractSpringContextTests

Convenient superclass for tests depending on a Spring context. The test instance itself is populated by Dependency Injection.

Really for integration testing, not unit testing. You should not normally use the Spring container for unit tests: simply populate your POJOs in plain JUnit tests!

This supports two modes of populating the test:

This class will normally 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.

If you don't want this behavior, you can override the contextKey() method, most likely to return the test class. In conjunction with this you would probably override the getContext method, which by default loads the locations specified in the getConfigLocations() method.

Since:
1.1.1
Author:
Rod Johnson
See Also:
setDirty(), contextKey(), AbstractSpringContextTests.getContext(java.lang.Object), getConfigLocations()

Field Summary
protected  ConfigurableApplicationContext applicationContext
          Application context this test will run against.
protected  String[] managedVariableNames
           
 
Fields inherited from class org.springframework.test.AbstractSpringContextTests
logger
 
Constructor Summary
AbstractDependencyInjectionSpringContextTests()
           
 
Method Summary
protected  Object contextKey()
          Return a key for this context.
protected abstract  String[] getConfigLocations()
          Subclasses must implement this method to return the locations of their config files.
 int getLoadCount()
           
protected  void initManagedVariableNames()
           
 boolean isDependencyCheck()
           
 boolean isPopulateProtectedVariables()
           
protected  ConfigurableApplicationContext loadContextLocations(String[] locations)
          Subclasses can invoke this to get a context key for the given location.
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 populateProtectedVariables()
           
 void setDependencyCheck(boolean dependencyCheck)
          Set whether or not dependency checking should be performed for test properties set by Dependency Injection.
 void setDirty()
          Called to say that the "applicationContext" instance variable is dirty and should be reloaded.
 void setPopulateProtectedVariables(boolean populateFields)
          Set whether to populate protected variables of this test case.
protected  void setUp()
           
protected  void tearDown()
          Reload the context if it's marked as dirty.
 
Methods inherited from class org.springframework.test.AbstractSpringContextTests
contextKeyString, getContext, hasCachedContext, loadContext, setDirty
 
Methods inherited from class junit.framework.TestCase
countTestCases, createResult, getName, run, run, runBare, 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.


managedVariableNames

protected String[] managedVariableNames
Constructor Detail

AbstractDependencyInjectionSpringContextTests

public AbstractDependencyInjectionSpringContextTests()
Method Detail

setPopulateProtectedVariables

public void setPopulateProtectedVariables(boolean populateFields)
Set whether to populate protected variables of this test case. Default is "false".


isPopulateProtectedVariables

public boolean isPopulateProtectedVariables()

setDependencyCheck

public void setDependencyCheck(boolean dependencyCheck)
Set whether or not dependency checking should be performed for test properties set by Dependency Injection. The default is true, meaning that tests cannot be run unless all properties are populated.

Parameters:
dependencyCheck - whether or not to perform dependency checking on all object properties.

isDependencyCheck

public boolean isDependencyCheck()

getLoadCount

public final int getLoadCount()

setDirty

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


setUp

protected final void setUp()
                    throws Exception
Overrides:
setUp in class junit.framework.TestCase
Throws:
Exception

contextKey

protected Object contextKey()
Return a key for this context. Usually based on config locations, but a subclass overriding buildContext() might want to return its class.


loadContextLocations

protected ConfigurableApplicationContext loadContextLocations(String[] locations)
Description copied from class: AbstractSpringContextTests
Subclasses can invoke this to get a context key for the given location. This doesn't affect the applicationContext instance variable in this class. Dependency Injection cannot be applied from such contexts.

Overrides:
loadContextLocations in class AbstractSpringContextTests

initManagedVariableNames

protected void initManagedVariableNames()
                                 throws IllegalAccessException
Throws:
IllegalAccessException

populateProtectedVariables

protected void populateProtectedVariables()
                                   throws IllegalAccessException
Throws:
IllegalAccessException

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

tearDown

protected final void tearDown()
Reload the context if it's marked as dirty.

Overrides:
tearDown in class junit.framework.TestCase
See Also:
onTearDown()

onTearDown

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

Throws:
Exception

getConfigLocations

protected abstract String[] getConfigLocations()
Subclasses must implement this method to return the locations of their config files. 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.

Returns:
an array of config locations


Copyright (c) 2002-2005 The Spring Framework Project.