|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.unitils.spring.SpringModule
public class SpringModule
A module for Spring enabling a test class by offering an easy way to load application contexts and an easy way of retrieving beans from the context and injecting them in the test.
The application context loading can be achieved by using theSpringApplicationContext
annotation. These
contexts are cached, so a context will be reused when possible. For example suppose a superclass loads a context and
a test-subclass wants to use this context, it will not create a new one. invalidateApplicationContext(java.lang.Class>...)
}
can be used to force a reloading of a context if needed.
Spring bean retrieval can be done by annotating the corresponding fields in the test with following
annotations: SpringBean
, SpringBeanByName
and SpringBeanByType
.
See the javadoc of these annotations for more info on how you can use them.
Nested Class Summary | |
---|---|
protected class |
SpringModule.SpringTestListener
The TestListener for this module |
Field Summary | |
---|---|
static String |
PROPKEY_APPLICATION_CONTEXT_FACTORY_CLASS_NAME
|
Constructor Summary | |
---|---|
SpringModule()
|
Method Summary | ||
---|---|---|
void |
afterInit()
No after initialization needed for this module |
|
org.springframework.context.ApplicationContext |
getApplicationContext(Object testObject)
Gets the application context for this test. |
|
protected DatabaseModule |
getDatabaseModule()
|
|
Object |
getSpringBean(Object testObject,
String name)
Gets the spring bean with the given name. |
|
|
getSpringBeanByType(Object testObject,
Class<T> type)
Gets the spring bean with the given type. |
|
TestListener |
getTestListener()
Creates the test listener for this module. |
|
void |
init(Properties configuration)
Initializes this module using the given configuration |
|
void |
injectApplicationContext(Object testObject)
Gets the application context for this class and sets it on the fields and setter methods that are annotated with SpringApplicationContext . |
|
void |
injectSpringBeans(Object testObject)
Injects spring beans into all fields that are annotated with SpringBean . |
|
void |
injectSpringBeansByName(Object testObject)
Injects spring beans into all fields that are annotated with SpringBeanByName . |
|
void |
injectSpringBeansByType(Object testObject)
Injects spring beans into all fields methods that are annotated with SpringBeanByType . |
|
void |
invalidateApplicationContext(Class<?>... classes)
Forces the reloading of the application context the next time that it is requested. |
|
boolean |
isApplicationContextConfiguredFor(Object testObject)
|
|
protected boolean |
isDatabaseModuleEnabled()
|
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static final String PROPKEY_APPLICATION_CONTEXT_FACTORY_CLASS_NAME
Constructor Detail |
---|
public SpringModule()
Method Detail |
---|
public void init(Properties configuration)
init
in interface Module
configuration
- The configuration, not nullpublic void afterInit()
afterInit
in interface Module
public Object getSpringBean(Object testObject, String name)
SpringApplicationContext
,
determines the application context in which to look for the bean.
A UnitilsException is thrown when the no bean could be found for the given name.
testObject
- The test instance, not nullname
- The name, not null
public <T> T getSpringBeanByType(Object testObject, Class<T> type)
SpringApplicationContext
,
determines the application context in which to look for the bean.
If more there is not exactly 1 possible bean assignment, an UnitilsException will be thrown.
testObject
- The test instance, not nulltype
- The type, not null
public boolean isApplicationContextConfiguredFor(Object testObject)
testObject
- The test object
public org.springframework.context.ApplicationContext getApplicationContext(Object testObject)
SpringApplicationContext
annotation.
If a class level SpringApplicationContext
annotation is found, the passed locations will be loaded using
a ClassPathXmlApplicationContext
.
Custom creation methods can be created by annotating them with SpringApplicationContext
. They
should have an ApplicationContext
as return type and either no or exactly 1 argument of type
ApplicationContext
. In the latter case, the current configured application context is passed as the argument.
A UnitilsException will be thrown if no context could be retrieved or created.
testObject
- The test instance, not null
public void invalidateApplicationContext(Class<?>... classes)
classes
- The classes for which to reset the contextspublic void injectApplicationContext(Object testObject)
SpringApplicationContext
. If no application context could be created, an
UnitilsException will be raised.
testObject
- The test instance, not nullpublic void injectSpringBeans(Object testObject)
SpringBean
.
testObject
- The test instance, not nullpublic void injectSpringBeansByType(Object testObject)
SpringBeanByType
.
testObject
- The test instance, not nullpublic void injectSpringBeansByName(Object testObject)
SpringBeanByName
.
testObject
- The test instance, not nullprotected boolean isDatabaseModuleEnabled()
protected DatabaseModule getDatabaseModule()
public TestListener getTestListener()
Module
getTestListener
in interface Module
SpringModule.SpringTestListener
for this module
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |