|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object junit.framework.Assert junit.framework.TestCase org.springframework.test.AbstractSpringContextTests org.springframework.test.AbstractDependencyInjectionSpringContextTests
public abstract class AbstractDependencyInjectionSpringContextTests
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.
WARNING: When doing integration tests from within Eclipse, only use classpath resource URLs. Else, you may see misleading failures when changing context locations.
setDirty()
,
contextKey()
,
AbstractSpringContextTests.getContext(java.lang.Object)
,
getConfigLocations()
Field Summary | |
---|---|
protected ConfigurableApplicationContext |
applicationContext
Application context this test will run against |
static int |
AUTOWIRE_BY_NAME
Constant that indicates autowiring bean properties by name. |
static int |
AUTOWIRE_BY_TYPE
Constant that indicates autowiring bean properties by type. |
static int |
AUTOWIRE_NO
Constant that indicates no autowiring at all. |
protected String[] |
managedVariableNames
|
Fields inherited from class org.springframework.test.AbstractSpringContextTests |
---|
logger |
Constructor Summary | |
---|---|
AbstractDependencyInjectionSpringContextTests()
Default constructor for AbstractDependencyInjectionSpringContextTests. |
|
AbstractDependencyInjectionSpringContextTests(String name)
Constructor for AbstractDependencyInjectionSpringContextTests with a JUnit name. |
Method Summary | |
---|---|
protected Object |
contextKey()
Return a key for this context. |
int |
getAutowireMode()
Return the autowire mode for test properties set by Dependency Injection. |
protected abstract String[] |
getConfigLocations()
Subclasses must implement this method to return the locations of their config files. |
int |
getLoadCount()
Return the current number of context load attempts. |
protected void |
initManagedVariableNames()
|
boolean |
isDependencyCheck()
Return whether or not dependency checking should be performed for test properties set by Dependency Injection. |
boolean |
isPopulateProtectedVariables()
Return whether to populate protected variables of this test case. |
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 |
setAutowireMode(int autowireMode)
Set the autowire mode for test properties set by Dependency Injection. |
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 |
---|
public static final int AUTOWIRE_NO
setAutowireMode(int)
,
Constant Field Valuespublic static final int AUTOWIRE_BY_NAME
setAutowireMode(int)
,
Constant Field Valuespublic static final int AUTOWIRE_BY_TYPE
setAutowireMode(int)
,
Constant Field Valuesprotected ConfigurableApplicationContext applicationContext
protected String[] managedVariableNames
Constructor Detail |
---|
public AbstractDependencyInjectionSpringContextTests()
public AbstractDependencyInjectionSpringContextTests(String name)
Method Detail |
---|
public void setPopulateProtectedVariables(boolean populateFields)
public boolean isPopulateProtectedVariables()
public void setAutowireMode(int autowireMode)
The default is "AUTOWIRE_BY_TYPE". Can be set to "AUTOWIRE_BY_NAME" or "AUTOWIRE_NO" instead.
AUTOWIRE_BY_TYPE
,
AUTOWIRE_BY_NAME
,
AUTOWIRE_NO
public int getAutowireMode()
public void setDependencyCheck(boolean dependencyCheck)
The default is "true", meaning that tests cannot be run unless all properties are populated.
public boolean isDependencyCheck()
public final int getLoadCount()
public void setDirty()
protected final void setUp() throws Exception
protected Object contextKey()
protected ConfigurableApplicationContext loadContextLocations(String[] locations)
AbstractSpringContextTests
loadContextLocations
in class AbstractSpringContextTests
protected void initManagedVariableNames() throws IllegalAccessException
IllegalAccessException
protected void populateProtectedVariables() throws IllegalAccessException
IllegalAccessException
protected void onSetUp() throws Exception
setUp()
method, which is final in this class.
This implementation does nothing.
Exception
- simply let any exception propagateprotected final void tearDown()
onTearDown()
protected void onTearDown() throws Exception
Exception
- simply let any exception propagateprotected abstract String[] getConfigLocations()
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |