Class 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 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, Tim Ducheyne
    • Constructor Detail

      • InjectModule

        public InjectModule()
    • Method Detail

      • init

        public void init​(Properties configuration)
        Initializes this module using the given configuration.
        Specified by:
        init in interface Module
        Parameters:
        configuration - The configuration, not null
      • afterInit

        public void afterInit()
        No after initialization needed for this module
        Specified by:
        afterInit in interface Module
      • createTestedObjectsIfNull

        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. 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 null
        testedObjectField - 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 with InjectInto.
        Parameters:
        test - The instance to inject into, not null
      • injectAllByType

        public void injectAllByType​(Object test)
        Auto-injects all fields that are annotated with InjectIntoByType
        Parameters:
        test - The instance to inject into, not null
      • injectAllStatic

        public void injectAllStatic​(Object test)
        Injects all fields that are annotated with InjectIntoStatic.
        Parameters:
        test - The instance to inject into, not null
      • injectAllStaticByType

        public void injectAllStaticByType​(Object test)
        Auto-injects all fields that are annotated with InjectIntoStaticByType
        Parameters:
        test - The instance to inject into, not null
      • 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 with TestedObject
        Parameters:
        test - The instance to inject into, not null
        fieldToInject - 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 null
        fieldToInjectStatic - 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 with TestedObject
        Parameters:
        test - The instance to inject into, not null
        fieldToInject - 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 with TestedObject
        Parameters:
        test - The instance to inject into, not null
        fieldToAutoInjectStatic - 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 null
        fieldToInject - 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 null
        fieldToInject - 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 null
        property - The OGNL expression that defines where the object will be injected, null for auto inject
        fieldType - The type, not null
        propertyAccess - The access type in case auto injection is used
        oldValue - The value that was replaced during the injection
        restore - 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 with TestedObject.
        Parameters:
        annotationClass - The class of the annotation, not null
        annotatedField - The annotated field, not null
        targetNames - The explicit target name or empty string for TestedObject targets
        test - 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 null
        annotatedField - The annotated field, not null
        errorDescription - A custom description, not null
        Returns:
        A situated error message