org.mockito.internal.configuration.injection
Class PropertyAndSetterInjection
java.lang.Object
org.mockito.internal.configuration.injection.MockInjectionStrategy
org.mockito.internal.configuration.injection.PropertyAndSetterInjection
public class PropertyAndSetterInjection
- extends MockInjectionStrategy
Inject mocks using first setters then fields, if no setters available.
Algorithm :
for each field annotated by @InjectMocks
- copy mocks set
- initialize field annotated by @InjectMocks
- for each field in @InjectMocks type ordered from sub-type to super-type
- 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)
- 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.
Method Summary |
boolean |
processInjection(java.lang.reflect.Field field,
java.lang.Object fieldOwner,
java.util.Set<java.lang.Object> mockCandidates)
Process actual injection. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
PropertyAndSetterInjection
public PropertyAndSetterInjection()
processInjection
public boolean processInjection(java.lang.reflect.Field field,
java.lang.Object fieldOwner,
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:
field
- Field needing injectionfieldOwner
- Field owner instance.mockCandidates
- Pool of mocks to inject.
- Returns:
true
if injection occurred, false
otherwise