| java.lang.Object org.unitils.inject.InjectModule
InjectModule | public class InjectModule implements Module(Code) | | 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 with
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 author: Filip Neven author: Tim Ducheyne |
Inner Class :protected class InjectTestListener extends TestListener | |
Method Summary | |
protected void | createObjectForField(Object testObject, Field testedObjectField) | public TestListener | createTestListener() | public 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 String | getSituatedErrorMessage(Class<? extends Annotation> annotationClass, Field annotatedField, String errorDescription) Given the errorDescription, returns a situated error message, i.e. | 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. | public void | init(Properties configuration) Initializes this module using the given configuration. | protected void | inject(Object test, Field fieldToInject) Injects the fieldToInject. | public void | injectAll(Object test) Injects all fields that are annotated with
InjectInto . | public void | injectAllByType(Object test) | public void | injectAllStatic(Object test) Injects all fields that are annotated with
InjectIntoStatic . | public void | injectAllStaticByType(Object test) | 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. | public void | injectObjects(Object test) | 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. | public void | restoreStaticInjectedObjects() Restores the values that were stored using
InjectModule.storeValueToRestoreAfterTest . | 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. |
createObjectForField | protected void createObjectForField(Object testObject, Field testedObjectField)(Code) | | Creates an objects of the given fields' declared type and assigns it to this field on the given testObject
Parameters: testObject - The test instance, not null Parameters: testedObjectField - The tested object field, not null |
createTestedObjectsIfNull | public void createTestedObjectsIfNull(Object testObject)(Code) | | 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. 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.
Parameters: testObject - The test instance, not null |
getSituatedErrorMessage | protected String getSituatedErrorMessage(Class<? extends Annotation> annotationClass, Field annotatedField, String errorDescription)(Code) | | Given the errorDescription, returns a situated error message, i.e. specifying the annotated field and the
annotation type that was used.
Parameters: annotationClass - The injection annotation, not null Parameters: annotatedField - The annotated field, not null Parameters: errorDescription - A custom description, not null A situated error message |
getTargets | protected List<Object> getTargets(Class<? extends Annotation> annotationClass, Field annotatedField, String targetName, Object test)(Code) | | Returns the target(s) for the injection, given the specified name of the target and the test object. If
targetName is not equal to an empty string, the targets are the testObject's fields that are annotated with
TestedObject .
Parameters: annotationClass - The class of the annotation, not null Parameters: annotatedField - The annotated field, not null Parameters: targetName - The explicit target name or empty string for TestedObject targets Parameters: test - The test instance The target(s) for the injection |
init | public void init(Properties configuration)(Code) | | Initializes this module using the given configuration.
Parameters: configuration - The configuration, not null |
inject | protected void inject(Object test, Field fieldToInject)(Code) | | Injects the fieldToInject. The target is either an explicitly specified target field of the test, or into the
field(s) that is/are annotated with
TestedObject Parameters: test - The instance to inject into, not null Parameters: fieldToInject - The field from which the value is injected into the target, not null |
injectAll | public void injectAll(Object test)(Code) | | Injects all fields that are annotated with
InjectInto .
Parameters: test - The instance to inject into, not null |
injectAllByType | public void injectAllByType(Object test)(Code) | | Auto-injects all fields that are annotated with
InjectIntoByType Parameters: test - The instance to inject into, not null |
injectAllStatic | public void injectAllStatic(Object test)(Code) | | Injects all fields that are annotated with
InjectIntoStatic .
Parameters: test - The instance to inject into, not null |
injectAllStaticByType | public void injectAllStaticByType(Object test)(Code) | | Auto-injects all fields that are annotated with
InjectIntoStaticByType Parameters: test - The instance to inject into, not null |
injectByType | protected void injectByType(Object test, Field fieldToInject)(Code) | | Auto-injects the fieldToInject by trying to match the fields declared type with a property of the target.
The target is either an explicitly specified target field of the test, or the field(s) that is/are annotated with
TestedObject Parameters: test - The instance to inject into, not null Parameters: fieldToInject - The field from which the value is injected into the target, not null |
injectObjects | public void injectObjects(Object test)(Code) | | Performs all supported kinds of injection on the given object's fields
Parameters: test - The instance to inject into, not null |
injectStatic | protected void injectStatic(Object test, Field fieldToInjectStatic)(Code) | | Injects the fieldToAutoInjectStatic into the specified target class.
Parameters: test - Instance to inject into, not null Parameters: fieldToInjectStatic - The field from which the value is injected into the target, not null |
injectStaticByType | protected void injectStaticByType(Object test, Field fieldToAutoInjectStatic)(Code) | | Auto-injects the fieldToInject by trying to match the fields declared type with a property of the target class.
The target is either an explicitly specified target field of the test, or the field that is annotated with
TestedObject Parameters: test - The instance to inject into, not null Parameters: fieldToAutoInjectStatic - The field from which the value is injected into the target, not null |
restore | protected void restore(ValueToRestore valueToRestore)(Code) | | Restores the given value.
Parameters: valueToRestore - the value, not null |
storeValueToRestoreAfterTest | protected void storeValueToRestoreAfterTest(Class> targetClass, String property, Class> fieldType, PropertyAccess propertyAccess, Object oldValue, Restore restore)(Code) | | Stores the old value that was replaced during the injection so that it can be restored after the test was
performed. The value that is stored depends on the restore value: OLD_VALUE will store the value that was replaced,
NULL_OR_0_VALUE will store 0 or null depeding whether it is a primitive or not, NO_RESTORE stores nothing.
Parameters: targetClass - The target class, not null Parameters: property - The OGNL expression that defines where the object will be injected, null for auto inject Parameters: fieldType - The type, not null Parameters: propertyAccess - The access type in case auto injection is used Parameters: oldValue - The value that was replaced during the injection Parameters: restore - The type of reset, not DEFAULT |
|
|