Class PropertyAndSetterInjection


  • public class PropertyAndSetterInjection
    extends MockInjectionStrategy
    Inject mocks using first setters then fields, if no setters available.

    Algorithm :
    for each field annotated by @InjectMocks

    • initialize field annotated by @InjectMocks
    • for each fields of a class in @InjectMocks type hierarchy
      • make a copy of mock candidates
      • order fields from sub-type to super-type, then by field name
      • for the list of fields in a class try two passes of :
        • find mock candidate by type
        • if more than *one* candidate find mock candidate on name
        • if one mock candidate then
          • set mock by property setter if possible
          • else set mock by field injection
        • remove mock from mocks copy (mocks are just injected once in a class)
        • remove injected field from list of class fields
      • else don't fail, user will then provide dependencies

    Note: If the field needing injection is not initialized, the strategy tries to create one using a no-arg constructor of the field type.

    • Constructor Detail

      • PropertyAndSetterInjection

        public PropertyAndSetterInjection()
    • Method Detail

      • processInjection

        public boolean processInjection​(java.lang.reflect.Field injectMocksField,
                                        java.lang.Object injectMocksFieldOwner,
                                        java.util.Set<java.lang.Object> mockCandidates)
        Description copied from class: MockInjectionStrategy
        Process actual injection.

        Don't call this method directly, instead call MockInjectionStrategy.process(Field, Object, Set)

        Specified by:
        processInjection in class MockInjectionStrategy
        Parameters:
        injectMocksField - Field needing injection
        injectMocksFieldOwner - Field owner instance.
        mockCandidates - Pool of mocks to inject.
        Returns:
        true if injection occurred, false otherwise
      • initializeInjectMocksField

        private FieldInitializationReport initializeInjectMocksField​(java.lang.reflect.Field field,
                                                                     java.lang.Object fieldOwner)
      • injectMockCandidates

        private boolean injectMockCandidates​(java.lang.Class<?> awaitingInjectionClazz,
                                             java.lang.Object injectee,
                                             java.util.Set<java.lang.Object> mocks)
      • injectMockCandidatesOnFields

        private boolean injectMockCandidatesOnFields​(java.util.Set<java.lang.Object> mocks,
                                                     java.lang.Object injectee,
                                                     boolean injectionOccurred,
                                                     java.util.List<java.lang.reflect.Field> orderedCandidateInjecteeFields)
      • orderedInstanceFieldsFrom

        private java.util.List<java.lang.reflect.Field> orderedInstanceFieldsFrom​(java.lang.Class<?> awaitingInjectionClazz)