|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.unitils.inject.InjectModule
public class InjectModule
Module for injecting annotated objects into other objects. The intended usage is to inject mock objects, but it can be used for regular objects too.
Both explicit injection and automatic injection by type are supported. An object annotated withInjectInto
is
explicitly injected into a target object. An object annotated with InjectIntoByType
is automatically injected into a
target property with the same type as the declared type of the annotated object.
Explicit and automatic injection into static fields is also supported, by means of the InjectIntoStatic
and InjectIntoStaticByType
annotations.
The target object can either be specified explicitly, or implicitly by annotating an object with TestedObject
Nested Class Summary | |
---|---|
protected class |
InjectModule.InjectTestListener
The TestListener for this module |
Constructor Summary | |
---|---|
InjectModule()
|
Method Summary | |
---|---|
void |
afterInit()
No after initialization needed for this module |
protected void |
createObjectForField(Object testObject,
Field testedObjectField)
Creates an objects of the given fields' declared type and assigns it to this field on the given testObject |
void |
createTestedObjectsIfNull(Object testObject)
For all fields annotated with TestedObject that are still null after the test fixture, an object is
created of the field's declared type and assigned to the field. |
protected Object |
getObjectToInject(Object test,
Field fieldToInject)
Gets the value from the given field. |
protected Type |
getObjectToInjectType(Object test,
Field fieldToInject)
Gets the type of the given field. |
protected String |
getSituatedErrorMessage(Class<? extends Annotation> annotationClass,
Field annotatedField,
String errorDescription)
Given the errorDescription, returns a situated error message, i.e. specifying the annotated field and the annotation type that was used. |
protected Object |
getTarget(Object test,
Field field)
|
protected List<Object> |
getTargets(Class<? extends Annotation> annotationClass,
Field annotatedField,
String targetName,
Object test)
Returns the target(s) for the injection, given the specified name of the target and the test object. |
TestListener |
getTestListener()
Creates the test listener for this module. |
void |
init(Properties configuration)
Initializes this module using the given configuration. |
protected void |
inject(Object test,
Field fieldToInject)
Injects the fieldToInject. |
void |
injectAll(Object test)
Injects all fields that are annotated with InjectInto . |
void |
injectAllByType(Object test)
Auto-injects all fields that are annotated with InjectIntoByType |
void |
injectAllStatic(Object test)
Injects all fields that are annotated with InjectIntoStatic . |
void |
injectAllStaticByType(Object test)
Auto-injects all fields that are annotated with InjectIntoStaticByType |
protected void |
injectByType(Object test,
Field fieldToInject)
Auto-injects the fieldToInject by trying to match the fields declared type with a property of the target. |
void |
injectObjects(Object test)
Performs all supported kinds of injection on the given object's fields |
protected void |
injectStatic(Object test,
Field fieldToInjectStatic)
Injects the fieldToAutoInjectStatic into the specified target class. |
protected void |
injectStaticByType(Object test,
Field fieldToAutoInjectStatic)
Auto-injects the fieldToInject by trying to match the fields declared type with a property of the target class. |
protected void |
restore(ValueToRestore valueToRestore)
Restores the given value. |
void |
restoreStaticInjectedObjects()
Restores the values that were stored using storeValueToRestoreAfterTest(Class, String, Class, org.unitils.inject.util.PropertyAccess, Object, org.unitils.inject.util.Restore) . |
protected void |
storeValueToRestoreAfterTest(Class<?> targetClass,
String property,
Class<?> fieldType,
PropertyAccess propertyAccess,
Object oldValue,
Restore restore)
Stores the old value that was replaced during the injection so that it can be restored after the test was performed. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public InjectModule()
Method Detail |
---|
public void init(Properties configuration)
init
in interface Module
configuration
- The configuration, not nullpublic void afterInit()
afterInit
in interface Module
public void createTestedObjectsIfNull(Object testObject)
TestedObject
that are still null after the test fixture, an object is
created of the field's declared type and assigned to the field. If the field's declared type is an interface or
abstract class, or if the type doesn't have a default constructor, a warning is produced.
testObject
- The test instance, not nullprotected void createObjectForField(Object testObject, Field testedObjectField)
testObject
- The test instance, not nulltestedObjectField
- The tested object field, not nullpublic void injectObjects(Object test)
test
- The instance to inject into, not nullpublic void injectAll(Object test)
InjectInto
.
test
- The instance to inject into, not nullpublic void injectAllByType(Object test)
InjectIntoByType
test
- The instance to inject into, not nullpublic void injectAllStatic(Object test)
InjectIntoStatic
.
test
- The instance to inject into, not nullpublic void injectAllStaticByType(Object test)
InjectIntoStaticByType
test
- The instance to inject into, not nullpublic void restoreStaticInjectedObjects()
storeValueToRestoreAfterTest(Class, String, Class, org.unitils.inject.util.PropertyAccess, Object, org.unitils.inject.util.Restore)
.
protected void inject(Object test, Field fieldToInject)
TestedObject
test
- The instance to inject into, not nullfieldToInject
- The field from which the value is injected into the target, not nullprotected void injectStatic(Object test, Field fieldToInjectStatic)
test
- Instance to inject into, not nullfieldToInjectStatic
- The field from which the value is injected into the target, not nullprotected void injectByType(Object test, Field fieldToInject)
TestedObject
test
- The instance to inject into, not nullfieldToInject
- The field from which the value is injected into the target, not nullprotected void injectStaticByType(Object test, Field fieldToAutoInjectStatic)
TestedObject
test
- The instance to inject into, not nullfieldToAutoInjectStatic
- The field from which the value is injected into the target, not nullprotected Object getObjectToInject(Object test, Field fieldToInject)
test
- The test, not nullfieldToInject
- The field, not null
protected Type getObjectToInjectType(Object test, Field fieldToInject)
test
- The test, not nullfieldToInject
- The field, not null
protected void restore(ValueToRestore valueToRestore)
valueToRestore
- the value, not nullprotected void storeValueToRestoreAfterTest(Class<?> targetClass, String property, Class<?> fieldType, PropertyAccess propertyAccess, Object oldValue, Restore restore)
targetClass
- The target class, not nullproperty
- The OGNL expression that defines where the object will be injected, null for auto injectfieldType
- The type, not nullpropertyAccess
- The access type in case auto injection is usedoldValue
- The value that was replaced during the injectionrestore
- The type of reset, not DEFAULTprotected List<Object> getTargets(Class<? extends Annotation> annotationClass, Field annotatedField, String targetName, Object test)
TestedObject
.
annotationClass
- The class of the annotation, not nullannotatedField
- The annotated field, not nulltargetName
- The explicit target name or empty string for TestedObject targetstest
- The test instance
protected Object getTarget(Object test, Field field)
protected String getSituatedErrorMessage(Class<? extends Annotation> annotationClass, Field annotatedField, String errorDescription)
annotationClass
- The injection annotation, not nullannotatedField
- The annotated field, not nullerrorDescription
- A custom description, not null
public TestListener getTestListener()
Module
getTestListener
in interface Module
TestListener
for this module
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |