Search in sources :

Example 1 with FieldInitializationReport

use of org.mockito.internal.util.reflection.FieldInitializationReport in project gwt-test-utils by gwt-test-utils.

the class GwtSpyAnnotationEngine method process.

@Override
@SuppressWarnings("deprecation")
public // for MockitoAnnotations.Mock
void process(Class<?> context, Object testInstance) {
    Field[] fields = context.getDeclaredFields();
    for (Field field : fields) {
        if (field.isAnnotationPresent(Spy.class) && !field.isAnnotationPresent(InjectMocks.class)) {
            assertNoIncompatibleAnnotations(Spy.class, field, com.googlecode.gwt.test.Mock.class, Mock.class, org.mockito.MockitoAnnotations.Mock.class, Captor.class);
            Object instance = null;
            try {
                FieldInitializationReport report = new FieldInitializer(testInstance, field).initialize();
                instance = report.fieldInstance();
            } catch (MockitoException e) {
                new Reporter().cannotInitializeForSpyAnnotation(field.getName(), e);
            }
            try {
                if (new MockUtil().isMock(instance)) {
                    // instance has been spied earlier
                    // for example happens when MockitoAnnotations.initMocks is called two times.
                    Mockito.reset(instance);
                } else {
                    field.setAccessible(true);
                    field.set(testInstance, Mockito.mock(instance.getClass(), withSettings().spiedInstance(instance).defaultAnswer(Mockito.CALLS_REAL_METHODS).name(field.getName())));
                }
            } catch (IllegalAccessException e) {
                throw new MockitoException("Problems initiating spied field " + field.getName(), e);
            }
        }
    }
}
Also used : FieldInitializationReport(org.mockito.internal.util.reflection.FieldInitializationReport) MockUtil(org.mockito.internal.util.MockUtil) Reporter(org.mockito.exceptions.Reporter) FieldInitializer(org.mockito.internal.util.reflection.FieldInitializer) Field(java.lang.reflect.Field) MockitoException(org.mockito.exceptions.base.MockitoException)

Example 2 with FieldInitializationReport

use of org.mockito.internal.util.reflection.FieldInitializationReport in project mockito by mockito.

the class ConstructorInjection method processInjection.

public boolean processInjection(Field field, Object fieldOwner, Set<Object> mockCandidates) {
    try {
        SimpleArgumentResolver simpleArgumentResolver = new SimpleArgumentResolver(mockCandidates);
        FieldInitializationReport report = new FieldInitializer(fieldOwner, field, simpleArgumentResolver).initialize();
        return report.fieldWasInitializedUsingContructorArgs();
    } catch (MockitoException e) {
        if (e.getCause() instanceof InvocationTargetException) {
            Throwable realCause = e.getCause().getCause();
            throw fieldInitialisationThrewException(field, realCause);
        }
        // other causes should be fine
        return false;
    }
}
Also used : FieldInitializationReport(org.mockito.internal.util.reflection.FieldInitializationReport) MockitoException(org.mockito.exceptions.base.MockitoException) FieldInitializer(org.mockito.internal.util.reflection.FieldInitializer) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 3 with FieldInitializationReport

use of org.mockito.internal.util.reflection.FieldInitializationReport in project mockito by mockito.

the class PropertyAndSetterInjection method processInjection.

public boolean processInjection(Field injectMocksField, Object injectMocksFieldOwner, Set<Object> mockCandidates) {
    FieldInitializationReport report = initializeInjectMocksField(injectMocksField, injectMocksFieldOwner);
    // for each field in the class hierarchy
    boolean injectionOccurred = false;
    Class<?> fieldClass = report.fieldClass();
    Object fieldInstanceNeedingInjection = report.fieldInstance();
    while (fieldClass != Object.class) {
        injectionOccurred |= injectMockCandidates(fieldClass, fieldInstanceNeedingInjection, newMockSafeHashSet(mockCandidates));
        fieldClass = fieldClass.getSuperclass();
    }
    return injectionOccurred;
}
Also used : FieldInitializationReport(org.mockito.internal.util.reflection.FieldInitializationReport)

Aggregations

FieldInitializationReport (org.mockito.internal.util.reflection.FieldInitializationReport)3 MockitoException (org.mockito.exceptions.base.MockitoException)2 FieldInitializer (org.mockito.internal.util.reflection.FieldInitializer)2 Field (java.lang.reflect.Field)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Reporter (org.mockito.exceptions.Reporter)1 MockUtil (org.mockito.internal.util.MockUtil)1