Class AbstractSingleSpringContextTests
- java.lang.Object
-
- org.junit.Assert
-
- org.springframework.test.AbstractSpringContextTests
-
- org.springframework.test.AbstractSingleSpringContextTests
-
- Direct Known Subclasses:
AbstractDependencyInjectionSpringContextTests
@Deprecated public abstract class AbstractSingleSpringContextTests extends AbstractSpringContextTests
Deprecated.as of Spring 3.0, in favor of using the listener-based test context framework (org.springframework.test.context.junit38.AbstractJUnit38SpringContextTests
)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 theloadContext(Object)
method, which by default loads the locations specified in thegetConfigLocations()
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, Sam Brannen
- See Also:
getConfigLocations()
,contextKey()
,loadContext(Object)
,getApplicationContext()
-
-
Field Summary
Fields Modifier and Type Field Description protected org.springframework.context.ConfigurableApplicationContext
applicationContext
Deprecated.Application context this test will run against-
Fields inherited from class org.springframework.test.AbstractSpringContextTests
logger
-
-
Constructor Summary
Constructors Constructor Description AbstractSingleSpringContextTests()
Deprecated.Default constructor for AbstractSingleSpringContextTests.
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description protected Object
contextKey()
Deprecated.Return a key for this context.protected org.springframework.context.ConfigurableApplicationContext
createApplicationContext(String[] locations)
Deprecated.Create a SpringConfigurableApplicationContext
for use by this test.protected org.springframework.beans.factory.support.BeanDefinitionReader
createBeanDefinitionReader(org.springframework.context.support.GenericApplicationContext context)
Deprecated.Factory method for creating newBeanDefinitionReader
s for loading bean definitions into the suppliedcontext
.protected void
customizeBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory beanFactory)
Deprecated.Customize the internal bean factory of the ApplicationContext used by this test.org.springframework.context.ConfigurableApplicationContext
getApplicationContext()
Deprecated.Return the ApplicationContext that this base class manages; may benull
.protected String[]
getConfigLocations()
Deprecated.Subclasses can override this method to return the locations of their config files, unless they overridecontextKey()
andloadContext(Object)
instead.protected org.springframework.context.ConfigurableApplicationContext
loadContext(Object key)
Deprecated.This implementation assumes a key of type String array and loads a context from the given locations.protected org.springframework.context.ConfigurableApplicationContext
loadContextLocations(String[] locations)
Deprecated.Load a Spring ApplicationContext from the given config locations.protected void
onSetUp()
Deprecated.Subclasses can override this method in place of thesetUp()
method, which is final in this class.protected void
onTearDown()
Deprecated.Subclasses can override this to add custom behavior on teardown.protected void
prepareTestInstance()
Deprecated.Prepare this test instance, for example populating its fields.protected void
setDirty()
Deprecated.Called to say that the "applicationContext" instance variable is dirty and should be reloaded.protected void
setUp()
Deprecated.This implementation is final.protected void
tearDown()
Deprecated.This implementation is final.-
Methods inherited from class org.springframework.test.AbstractSpringContextTests
contextKeyString, getContext, getContextKeyToContextMap, isContextKeyEmpty, setDirty
-
Methods inherited from class org.junit.Assert
assertArrayEquals, assertArrayEquals, assertArrayEquals, assertArrayEquals, assertArrayEquals, assertArrayEquals, assertArrayEquals, assertArrayEquals, assertArrayEquals, assertArrayEquals, assertArrayEquals, assertArrayEquals, assertArrayEquals, assertArrayEquals, assertArrayEquals, assertArrayEquals, assertArrayEquals, assertArrayEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertFalse, assertFalse, assertNotEquals, assertNotEquals, assertNotEquals, assertNotEquals, assertNotEquals, assertNotEquals, assertNotEquals, assertNotEquals, assertNotNull, assertNotNull, assertNotSame, assertNotSame, assertNull, assertNull, assertSame, assertSame, assertThat, assertThat, assertThrows, assertThrows, assertTrue, assertTrue, fail, fail
-
-
-
-
Method Detail
-
setUp
protected final void setUp() throws Exception
Deprecated.This implementation is final. OverrideonSetUp
for custom behavior.
-
prepareTestInstance
protected void prepareTestInstance() throws Exception
Deprecated.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
-
onSetUp
protected void onSetUp() throws Exception
Deprecated.Subclasses can override this method in place of thesetUp()
method, which is final in this class.The default implementation does nothing.
- Throws:
Exception
- simply let any exception propagate
-
setDirty
protected void setDirty()
Deprecated.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
Deprecated.This implementation is final. OverrideonTearDown
for custom behavior.- Throws:
Exception
- See Also:
onTearDown()
-
onTearDown
protected void onTearDown() throws Exception
Deprecated.Subclasses can override this to add custom behavior on teardown.- Throws:
Exception
- simply let any exception propagate
-
contextKey
protected Object contextKey()
Deprecated.Return a key for this context. Default is the config location array as determined bygetConfigLocations()
.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
- See Also:
getConfigLocations()
-
loadContext
protected org.springframework.context.ConfigurableApplicationContext loadContext(Object key) throws Exception
Deprecated.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 thatcontextKey()
returns.- Specified by:
loadContext
in classAbstractSpringContextTests
- Parameters:
key
- the context key- Returns:
- the corresponding ApplicationContext instance (new)
- Throws:
Exception
- See Also:
getConfigLocations()
-
loadContextLocations
protected org.springframework.context.ConfigurableApplicationContext loadContextLocations(String[] locations) throws Exception
Deprecated.Load a Spring ApplicationContext from the given config locations.The default implementation creates a standard
GenericApplicationContext
, allowing for customizing the internal bean factory throughcustomizeBeanFactory(org.springframework.beans.factory.support.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- See Also:
createApplicationContext(String[])
-
createApplicationContext
protected org.springframework.context.ConfigurableApplicationContext createApplicationContext(String[] locations)
Deprecated.Create a SpringConfigurableApplicationContext
for use by this test.The default implementation creates a standard
GenericApplicationContext
instance, calls the#prepareApplicationContext
prepareApplicationContext} method and thecustomizeBeanFactory
method to allow for customizing the context and its DefaultListableBeanFactory, populates the context from the specified configlocations
through the configuredBeanDefinitionReader
, and finallyrefreshes
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
- See Also:
loadContextLocations(String[])
,customizeBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory)
,createBeanDefinitionReader(org.springframework.context.support.GenericApplicationContext)
-
customizeBeanFactory
protected void customizeBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory beanFactory)
Deprecated.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- See Also:
loadContextLocations(java.lang.String[])
,createApplicationContext(java.lang.String[])
,DefaultListableBeanFactory.setAllowBeanDefinitionOverriding(boolean)
,DefaultListableBeanFactory.setAllowEagerClassLoading(boolean)
,AbstractAutowireCapableBeanFactory.setAllowCircularReferences(boolean)
,AbstractAutowireCapableBeanFactory.setAllowRawInjectionDespiteWrapping(boolean)
-
createBeanDefinitionReader
protected org.springframework.beans.factory.support.BeanDefinitionReader createBeanDefinitionReader(org.springframework.context.support.GenericApplicationContext context)
Deprecated.Factory method for creating newBeanDefinitionReader
s for loading bean definitions into the suppliedcontext
.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
- See Also:
createApplicationContext(String[])
,BeanDefinitionReader
,XmlBeanDefinitionReader
-
getConfigLocations
protected String[] getConfigLocations()
Deprecated.Subclasses can override this method to return the locations of their config files, unless they overridecontextKey()
andloadContext(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
- See Also:
#getConfigPaths()
,ResourceLoader.getResource(String)
-
getApplicationContext
public final org.springframework.context.ConfigurableApplicationContext getApplicationContext()
Deprecated.Return the ApplicationContext that this base class manages; may benull
.
-
-