Package org.unitils.inject
Class InjectModule
- java.lang.Object
-
- org.unitils.inject.InjectModule
-
- All Implemented Interfaces:
Module
public class InjectModule extends Object implements Module
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 withInjectIntois explicitly injected into a target object. An object annotated withInjectIntoByTypeis 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 theInjectIntoStaticandInjectIntoStaticByTypeannotations. The target object can either be specified explicitly, or implicitly by annotating an object withTestedObject- Author:
- Filip Neven, Tim Ducheyne
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected classInjectModule.InjectTestListenerTheTestListenerfor this module
-
Constructor Summary
Constructors Constructor Description InjectModule()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidafterInit()No after initialization needed for this moduleprotected voidcreateObjectForField(Object testObject, Field testedObjectField)Creates an objects of the given fields' declared type and assigns it to this field on the given testObjectvoidcreateTestedObjectsIfNull(Object testObject)For all fields annotated withTestedObjectthat are still null after the test fixture, an object is created of the field's declared type and assigned to the field.protected ObjectgetObjectToInject(Object test, Field fieldToInject)Gets the value from the given field.protected TypegetObjectToInjectType(Object test, Field fieldToInject)Gets the type of the given field.protected StringgetSituatedErrorMessage(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 ObjectgetTarget(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.TestListenergetTestListener()voidinit(Properties configuration)Initializes this module using the given configuration.protected voidinject(Object test, Field fieldToInject)Injects the fieldToInject.voidinjectAll(Object test)Injects all fields that are annotated withInjectInto.voidinjectAllByType(Object test)Auto-injects all fields that are annotated withInjectIntoByTypevoidinjectAllStatic(Object test)Injects all fields that are annotated withInjectIntoStatic.voidinjectAllStaticByType(Object test)Auto-injects all fields that are annotated withInjectIntoStaticByTypeprotected voidinjectByType(Object test, Field fieldToInject)Auto-injects the fieldToInject by trying to match the fields declared type with a property of the target.voidinjectObjects(Object test)Performs all supported kinds of injection on the given object's fieldsprotected voidinjectStatic(Object test, Field fieldToInjectStatic)Injects the fieldToAutoInjectStatic into the specified target class.protected voidinjectStaticByType(Object test, Field fieldToAutoInjectStatic)Auto-injects the fieldToInject by trying to match the fields declared type with a property of the target class.protected voidrestore(ValueToRestore valueToRestore)Restores the given value.voidrestoreStaticInjectedObjects()Restores the values that were stored usingstoreValueToRestoreAfterTest(Class, String, Class, org.unitils.inject.util.PropertyAccess, Object, org.unitils.inject.util.Restore).protected voidstoreValueToRestoreAfterTest(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.
-
-
-
Method Detail
-
init
public void init(Properties configuration)
Initializes this module using the given configuration.
-
afterInit
public void afterInit()
No after initialization needed for this module
-
createTestedObjectsIfNull
public void createTestedObjectsIfNull(Object testObject)
For all fields annotated withTestedObjectthat 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
-
createObjectForField
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- Parameters:
testObject- The test instance, not nulltestedObjectField- The tested object field, not null
-
injectObjects
public void injectObjects(Object test)
Performs all supported kinds of injection on the given object's fields- Parameters:
test- The instance to inject into, not null
-
injectAll
public void injectAll(Object test)
Injects all fields that are annotated withInjectInto.- Parameters:
test- The instance to inject into, not null
-
injectAllByType
public void injectAllByType(Object test)
Auto-injects all fields that are annotated withInjectIntoByType- Parameters:
test- The instance to inject into, not null
-
injectAllStatic
public void injectAllStatic(Object test)
Injects all fields that are annotated withInjectIntoStatic.- Parameters:
test- The instance to inject into, not null
-
injectAllStaticByType
public void injectAllStaticByType(Object test)
Auto-injects all fields that are annotated withInjectIntoStaticByType- Parameters:
test- The instance to inject into, not null
-
restoreStaticInjectedObjects
public void restoreStaticInjectedObjects()
Restores the values that were stored usingstoreValueToRestoreAfterTest(Class, String, Class, org.unitils.inject.util.PropertyAccess, Object, org.unitils.inject.util.Restore).
-
inject
protected void inject(Object test, Field fieldToInject)
Injects the fieldToInject. The target is either an explicitly specified target field of the test, or into the field(s) that is/are annotated withTestedObject- Parameters:
test- The instance to inject into, not nullfieldToInject- The field from which the value is injected into the target, not null
-
injectStatic
protected void injectStatic(Object test, Field fieldToInjectStatic)
Injects the fieldToAutoInjectStatic into the specified target class.- Parameters:
test- Instance to inject into, not nullfieldToInjectStatic- The field from which the value is injected into the target, not null
-
injectByType
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. The target is either an explicitly specified target field of the test, or the field(s) that is/are annotated withTestedObject- Parameters:
test- The instance to inject into, not nullfieldToInject- The field from which the value is injected into the target, not null
-
injectStaticByType
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. The target is either an explicitly specified target field of the test, or the field that is annotated withTestedObject- Parameters:
test- The instance to inject into, not nullfieldToAutoInjectStatic- The field from which the value is injected into the target, not null
-
getObjectToInject
protected Object getObjectToInject(Object test, Field fieldToInject)
Gets the value from the given field. If the value is a holder for an object to inject, the wrapped object is returned. For example in case of a field declared as Mock, this will return the proxy of the mock instead of the mock itself. - Parameters:
test- The test, not nullfieldToInject- The field, not null- Returns:
- The object
-
getObjectToInjectType
protected Type getObjectToInjectType(Object test, Field fieldToInject)
Gets the type of the given field. If the field is a holder for an object to inject, the wrapped type is returned. For example in case of a field declared as Mock, this will return MyClass instead of Mock - Parameters:
test- The test, not nullfieldToInject- The field, not null- Returns:
- The object
-
restore
protected void restore(ValueToRestore valueToRestore)
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)
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 depending whether it is a primitive or not, NO_RESTORE stores nothing.- Parameters:
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 DEFAULT
-
getTargets
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. If targetName is not equal to an empty string, the targets are the testObject's fields that are annotated withTestedObject.- Parameters:
annotationClass- The class of the annotation, not nullannotatedField- The annotated field, not nulltargetNames- The explicit target name or empty string for TestedObject targetstest- The test instance- Returns:
- The target(s) for the injection
-
getSituatedErrorMessage
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.- Parameters:
annotationClass- The injection annotation, not nullannotatedField- The annotated field, not nullerrorDescription- A custom description, not null- Returns:
- A situated error message
-
getTestListener
public TestListener getTestListener()
- Specified by:
getTestListenerin interfaceModule- Returns:
- The
TestListenerfor this module
-
-