Search in sources :

Example 1 with MockScanner

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);
}
Also used : Field(java.lang.reflect.Field) MockScanner(org.mockito.internal.configuration.injection.scanner.MockScanner) InjectMocksScanner(org.mockito.internal.configuration.injection.scanner.InjectMocksScanner) HashSet(java.util.HashSet) Sets.newMockSafeHashSet(org.mockito.internal.util.collections.Sets.newMockSafeHashSet)

Example 2 with MockScanner

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
 * &#064;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();
            }
        }
    };
}
Also used : Field(java.lang.reflect.Field) MockScanner(org.mockito.internal.configuration.injection.scanner.MockScanner) InjectMocksScanner(org.mockito.internal.configuration.injection.scanner.InjectMocksScanner) ScopedMock(org.mockito.ScopedMock) HashSet(java.util.HashSet) Sets.newMockSafeHashSet(org.mockito.internal.util.collections.Sets.newMockSafeHashSet)

Aggregations

Field (java.lang.reflect.Field)2 HashSet (java.util.HashSet)2 InjectMocksScanner (org.mockito.internal.configuration.injection.scanner.InjectMocksScanner)2 MockScanner (org.mockito.internal.configuration.injection.scanner.MockScanner)2 Sets.newMockSafeHashSet (org.mockito.internal.util.collections.Sets.newMockSafeHashSet)2 ScopedMock (org.mockito.ScopedMock)1