org.mockito.internal.configuration.injection.PropertyAndSetterInjection Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mockito-all Show documentation
Show all versions of mockito-all Show documentation
Mock objects library for java
/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito.internal.configuration.injection;
import org.mockito.exceptions.Reporter;
import org.mockito.exceptions.base.MockitoException;
import org.mockito.internal.configuration.injection.filter.FinalMockCandidateFilter;
import org.mockito.internal.configuration.injection.filter.MockCandidateFilter;
import org.mockito.internal.configuration.injection.filter.NameBasedCandidateFilter;
import org.mockito.internal.configuration.injection.filter.TypeBasedCandidateFilter;
import org.mockito.internal.util.collections.ListUtil;
import org.mockito.internal.util.reflection.FieldInitializationReport;
import org.mockito.internal.util.reflection.FieldInitializer;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Modifier;
import java.util.*;
import static org.mockito.internal.util.collections.Sets.newMockSafeHashSet;
/**
* 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 rom 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.
*
*/
public class PropertyAndSetterInjection extends MockInjectionStrategy {
private final MockCandidateFilter mockCandidateFilter = new TypeBasedCandidateFilter(new NameBasedCandidateFilter(new FinalMockCandidateFilter()));
private final Comparator superTypesLast = new FieldTypeAndNameComparator();
private final ListUtil.Filter notFinalOrStatic = new ListUtil.Filter() {
public boolean isOut(Field object) {
return Modifier.isFinal(object.getModifiers()) || Modifier.isStatic(object.getModifiers());
}
};
public boolean processInjection(Field injectMocksField, Object injectMocksFieldOwner, Set
© 2015 - 2024 Weber Informatics LLC | Privacy Policy