AbstractJUnit4SpringContextTests
)@Deprecated public abstract class AbstractSingleSpringContextTests extends AbstractSpringContextTests
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.
getConfigLocations()
,
contextKey()
,
loadContext(Object)
,
getApplicationContext()
Modifier and Type | Field and Description |
---|---|
protected ConfigurableApplicationContext |
applicationContext
Deprecated.
Application context this test will run against
|
private int |
loadCount
Deprecated.
|
logger
Constructor and Description |
---|
AbstractSingleSpringContextTests()
Deprecated.
Default constructor for AbstractSingleSpringContextTests.
|
AbstractSingleSpringContextTests(java.lang.String name)
Deprecated.
Constructor for AbstractSingleSpringContextTests with a JUnit name.
|
Modifier and Type | Method and Description |
---|---|
protected java.lang.Object |
contextKey()
Deprecated.
Return a key for this context.
|
protected ConfigurableApplicationContext |
createApplicationContext(java.lang.String[] locations)
Deprecated.
Create a Spring
ConfigurableApplicationContext for use by this test. |
protected BeanDefinitionReader |
createBeanDefinitionReader(GenericApplicationContext context)
Deprecated.
Factory method for creating new
BeanDefinitionReader s for
loading bean definitions into the supplied
context . |
protected void |
customizeBeanFactory(DefaultListableBeanFactory beanFactory)
Deprecated.
Customize the internal bean factory of the ApplicationContext used by
this test.
|
ConfigurableApplicationContext |
getApplicationContext()
Deprecated.
Return the ApplicationContext that this base class manages; may be
null . |
protected java.lang.String[] |
getConfigLocations()
Deprecated.
Subclasses can override this method to return the locations of their
config files, unless they override
contextKey() and
loadContext(Object) instead. |
protected java.lang.String |
getConfigPath()
Deprecated.
Subclasses can override this method to return a single path to a config
file, relative to the concrete test class.
|
protected java.lang.String[] |
getConfigPaths()
Deprecated.
Subclasses can override this method to return paths to their config
files, relative to the concrete test class.
|
int |
getLoadCount()
Deprecated.
Return the current number of context load attempts.
|
protected ConfigurableApplicationContext |
loadContext(java.lang.Object key)
Deprecated.
This implementation assumes a key of type String array and loads a
context from the given locations.
|
protected ConfigurableApplicationContext |
loadContextLocations(java.lang.String[] locations)
Deprecated.
Load a Spring ApplicationContext from the given config locations.
|
protected void |
onSetUp()
Deprecated.
Subclasses can override this method in place of the
setUp()
method, which is final in this class. |
protected void |
onTearDown()
Deprecated.
Subclasses can override this to add custom behavior on teardown.
|
protected void |
prepareApplicationContext(GenericApplicationContext context)
Deprecated.
Prepare the GenericApplicationContext used by this test.
|
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.
|
addContext, contextKeyString, getContext, hasCachedContext, isContextKeyEmpty, setDirty
getDisabledTestCount, isDisabledInThisEnvironment, recordDisabled, runBare
protected ConfigurableApplicationContext applicationContext
private int loadCount
public AbstractSingleSpringContextTests()
public AbstractSingleSpringContextTests(java.lang.String name)
name
- the name of this text fixtureprotected final void setUp() throws java.lang.Exception
onSetUp
for custom behavior.java.lang.Exception
onSetUp()
protected void prepareTestInstance() throws java.lang.Exception
The default implementation does nothing.
java.lang.Exception
- in case of preparation failureprotected void onSetUp() throws java.lang.Exception
setUp()
method, which is final in this class.
The default implementation does nothing.
java.lang.Exception
- simply let any exception propagateprotected void setDirty()
protected final void tearDown() throws java.lang.Exception
onTearDown
for
custom behavior.java.lang.Exception
onTearDown()
protected void onTearDown() throws java.lang.Exception
java.lang.Exception
- simply let any exception propagateprotected java.lang.Object contextKey()
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.
getConfigLocations()
protected ConfigurableApplicationContext loadContext(java.lang.Object key) throws java.lang.Exception
If you override contextKey()
, you will typically have to
override this method as well, being able to handle the key type that
contextKey()
returns.
loadContext
in class AbstractSpringContextTests
key
- the context keyjava.lang.Exception
getConfigLocations()
protected ConfigurableApplicationContext loadContextLocations(java.lang.String[] locations) throws java.lang.Exception
The default implementation creates a standard
GenericApplicationContext
, allowing
for customizing the internal bean factory through
customizeBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory)
.
locations
- the config locations (as Spring resource locations,
e.g. full classpath locations or any kind of URL)java.lang.Exception
- if context loading failedcreateApplicationContext(String[])
protected ConfigurableApplicationContext createApplicationContext(java.lang.String[] locations)
ConfigurableApplicationContext
for use by this test.
The default implementation creates a standard GenericApplicationContext
instance, calls the prepareApplicationContext(org.springframework.context.support.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.
locations
- the config locations (as Spring resource locations,
e.g. full classpath locations or any kind of URL)loadContextLocations(String[])
,
customizeBeanFactory(DefaultListableBeanFactory)
,
createBeanDefinitionReader(GenericApplicationContext)
protected void prepareApplicationContext(GenericApplicationContext context)
The default implementation is empty. Can be overridden in subclasses to customize GenericApplicationContext's standard settings.
context
- the context for which the BeanDefinitionReader should be createdcreateApplicationContext(java.lang.String[])
,
GenericApplicationContext.setResourceLoader(org.springframework.core.io.ResourceLoader)
,
GenericApplicationContext.setId(java.lang.String)
protected void customizeBeanFactory(DefaultListableBeanFactory beanFactory)
The default implementation is empty. Can be overridden in subclasses to customize DefaultListableBeanFactory's standard settings.
beanFactory
- the newly created bean factory for this contextloadContextLocations(java.lang.String[])
,
createApplicationContext(java.lang.String[])
,
DefaultListableBeanFactory.setAllowBeanDefinitionOverriding(boolean)
,
DefaultListableBeanFactory.setAllowEagerClassLoading(boolean)
,
AbstractAutowireCapableBeanFactory.setAllowCircularReferences(boolean)
,
AbstractAutowireCapableBeanFactory.setAllowRawInjectionDespiteWrapping(boolean)
protected BeanDefinitionReader createBeanDefinitionReader(GenericApplicationContext context)
BeanDefinitionReader
s 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.
context
- the context for which the BeanDefinitionReader should be createdcreateApplicationContext(String[])
,
BeanDefinitionReader
,
XmlBeanDefinitionReader
protected java.lang.String[] getConfigLocations()
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()
.
getConfigPaths()
,
ResourceLoader.getResource(String)
protected java.lang.String[] getConfigPaths()
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()
.
getConfigPath()
,
Class.getResource(String)
protected java.lang.String getConfigPath()
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
.
getConfigPath()
,
Class.getResource(String)
public final ConfigurableApplicationContext getApplicationContext()
null
.public final int getLoadCount()