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;
}
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;
}
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;
}
Aggregations