Search in sources :

Example 1 with FieldReader

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

the class SpyOnInjectedFieldsHandler method processInjection.

@Override
protected boolean processInjection(Field field, Object fieldOwner, Set<Object> mockCandidates) {
    FieldReader fieldReader = new FieldReader(fieldOwner, field);
    // TODO refoctor : code duplicated in SpyAnnotationEngine
    if (!fieldReader.isNull() && field.isAnnotationPresent(Spy.class)) {
        try {
            Object instance = fieldReader.read();
            if (MockUtil.isMock(instance)) {
                // A. instance has been spied earlier
                // B. protect against multiple use of MockitoAnnotations.initMocks()
                Mockito.reset(instance);
            } else {
                Object mock = Mockito.mock(instance.getClass(), withSettings().spiedInstance(instance).defaultAnswer(Mockito.CALLS_REAL_METHODS).name(field.getName()));
                setField(fieldOwner, field, mock);
            }
        } catch (Exception e) {
            throw new MockitoException("Problems initiating spied field " + field.getName(), e);
        }
    }
    return false;
}
Also used : MockitoException(org.mockito.exceptions.base.MockitoException) FieldReader(org.mockito.internal.util.reflection.FieldReader) Spy(org.mockito.Spy) MockitoException(org.mockito.exceptions.base.MockitoException)

Example 2 with FieldReader

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

the class GwtMockScanner method scan.

/**
     * Scan and prepare mocks for the given <code>testClassInstance</code> and <code>clazz</code> in
     * the type hierarchy.
     *
     * @return A prepared set of mock
     */
private Set<Object> scan() {
    Set<Object> mocks = newMockSafeHashSet();
    for (Field field : clazz.getDeclaredFields()) {
        // mock or spies only
        FieldReader fieldReader = new FieldReader(instance, field);
        Object mockInstance = preparedMock(fieldReader.read(), field);
        if (mockInstance != null) {
            mocks.add(mockInstance);
        }
    }
    return mocks;
}
Also used : Field(java.lang.reflect.Field) FieldReader(org.mockito.internal.util.reflection.FieldReader)

Example 3 with FieldReader

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

the class MockScanner method scan.

/**
     * Scan and prepare mocks for the given <code>testClassInstance</code> and <code>clazz</code> in the type hierarchy.
     *
     * @return A prepared set of mock
     */
private Set<Object> scan() {
    Set<Object> mocks = newMockSafeHashSet();
    for (Field field : clazz.getDeclaredFields()) {
        // mock or spies only
        FieldReader fieldReader = new FieldReader(instance, field);
        Object mockInstance = preparedMock(fieldReader.read(), field);
        if (mockInstance != null) {
            mocks.add(mockInstance);
        }
    }
    return mocks;
}
Also used : Field(java.lang.reflect.Field) FieldReader(org.mockito.internal.util.reflection.FieldReader)

Aggregations

FieldReader (org.mockito.internal.util.reflection.FieldReader)3 Field (java.lang.reflect.Field)2 Spy (org.mockito.Spy)1 MockitoException (org.mockito.exceptions.base.MockitoException)1