public class InjectModule extends Object implements Module
InjectInto 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| Modifier and Type | Class and Description |
|---|---|
protected class |
InjectModule.InjectTestListener
The
TestListener for this module |
| Constructor and Description |
|---|
InjectModule() |
| Modifier and Type | Method and Description |
|---|---|
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[] targetNames,
Object test)
Returns the target(s) for the injection, given the specified name of the target and the test object.
|
TestListener |
getTestListener() |
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.
|
public void init(Properties configuration)
public void afterInit()
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)
InjectIntoByTypetest - The instance to inject into, not nullpublic void injectAllStatic(Object test)
InjectIntoStatic.test - The instance to inject into, not nullpublic void injectAllStaticByType(Object test)
InjectIntoStaticByTypetest - 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)
TestedObjecttest - 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)
TestedObjecttest - 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)
TestedObjecttest - 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 nullprotected Type getObjectToInjectType(Object test, Field fieldToInject)
test - The test, not nullfieldToInject - The field, not nullprotected 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[] targetNames, 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 instanceprotected 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 nullpublic TestListener getTestListener()
getTestListener in interface ModuleTestListener for this moduleCopyright © 2016. All Rights Reserved.