use of org.mockito.internal.configuration.injection.scanner.MockScanner in project mockito by mockito.
the class InjectingAnnotationEngine method injectMocks.
/**
* Initializes mock/spies dependencies for objects annotated with
* @InjectMocks for given testClassInstance.
* <p>
* See examples in javadoc for {@link MockitoAnnotations} class.
*
* @param testClassInstance
* Test class, usually <code>this</code>
*/
public void injectMocks(final Object testClassInstance) {
Class<?> clazz = testClassInstance.getClass();
Set<Field> mockDependentFields = new HashSet<Field>();
Set<Object> mocks = newMockSafeHashSet();
while (clazz != Object.class) {
new InjectMocksScanner(clazz).addTo(mockDependentFields);
new MockScanner(testClassInstance, clazz).addPreparedMocks(mocks);
onInjection(testClassInstance, clazz, mockDependentFields, mocks);
clazz = clazz.getSuperclass();
}
new DefaultInjectionEngine().injectMocksOnFields(mockDependentFields, mocks, testClassInstance);
}
use of org.mockito.internal.configuration.injection.scanner.MockScanner in project mockito by mockito.
the class InjectingAnnotationEngine method injectCloseableMocks.
/**
* Initializes mock/spies dependencies for objects annotated with
* @InjectMocks for given testClassInstance.
* <p>
* See examples in javadoc for {@link MockitoAnnotations} class.
*
* @param testClassInstance
* Test class, usually <code>this</code>
*/
private AutoCloseable injectCloseableMocks(final Object testClassInstance) {
Class<?> clazz = testClassInstance.getClass();
Set<Field> mockDependentFields = new HashSet<>();
Set<Object> mocks = newMockSafeHashSet();
while (clazz != Object.class) {
new InjectMocksScanner(clazz).addTo(mockDependentFields);
new MockScanner(testClassInstance, clazz).addPreparedMocks(mocks);
onInjection(testClassInstance, clazz, mockDependentFields, mocks);
clazz = clazz.getSuperclass();
}
new DefaultInjectionEngine().injectMocksOnFields(mockDependentFields, mocks, testClassInstance);
return () -> {
for (Object mock : mocks) {
if (mock instanceof ScopedMock) {
((ScopedMock) mock).closeOnDemand();
}
}
};
}
Aggregations