Search in sources :

Example 1 with ScopedMock

use of org.mockito.ScopedMock in project mockito by mockito.

the class IndependentAnnotationEngine method process.

@Override
public AutoCloseable process(Class<?> clazz, Object testInstance) {
    List<ScopedMock> scopedMocks = new ArrayList<>();
    Field[] fields = clazz.getDeclaredFields();
    for (Field field : fields) {
        boolean alreadyAssigned = false;
        for (Annotation annotation : field.getAnnotations()) {
            Object mock = createMockFor(annotation, field);
            if (mock instanceof ScopedMock) {
                scopedMocks.add((ScopedMock) mock);
            }
            if (mock != null) {
                throwIfAlreadyAssigned(field, alreadyAssigned);
                alreadyAssigned = true;
                final MemberAccessor accessor = Plugins.getMemberAccessor();
                try {
                    accessor.set(field, testInstance, mock);
                } catch (Exception e) {
                    for (ScopedMock scopedMock : scopedMocks) {
                        scopedMock.close();
                    }
                    throw new MockitoException("Problems setting field " + field.getName() + " annotated with " + annotation, e);
                }
            }
        }
    }
    return () -> {
        for (ScopedMock scopedMock : scopedMocks) {
            scopedMock.closeOnDemand();
        }
    };
}
Also used : Field(java.lang.reflect.Field) MemberAccessor(org.mockito.plugins.MemberAccessor) MockitoException(org.mockito.exceptions.base.MockitoException) ScopedMock(org.mockito.ScopedMock) ArrayList(java.util.ArrayList) Annotation(java.lang.annotation.Annotation) MockitoException(org.mockito.exceptions.base.MockitoException)

Example 2 with ScopedMock

use of org.mockito.ScopedMock in project mockito by mockito.

the class MockitoExtension method resolveParameter.

@Override
@SuppressWarnings("unchecked")
public Object resolveParameter(ParameterContext parameterContext, ExtensionContext context) throws ParameterResolutionException {
    final Parameter parameter = parameterContext.getParameter();
    Object mock = MockAnnotationProcessor.processAnnotationForMock(parameterContext.findAnnotation(Mock.class).get(), parameter.getType(), parameter::getParameterizedType, parameter.getName());
    if (mock instanceof ScopedMock) {
        context.getStore(MOCKITO).get(MOCKS, Set.class).add(mock);
    }
    return mock;
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) ScopedMock(org.mockito.ScopedMock) Parameter(java.lang.reflect.Parameter)

Example 3 with ScopedMock

use of org.mockito.ScopedMock 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

ScopedMock (org.mockito.ScopedMock)3 Field (java.lang.reflect.Field)2 HashSet (java.util.HashSet)2 Annotation (java.lang.annotation.Annotation)1 Parameter (java.lang.reflect.Parameter)1 ArrayList (java.util.ArrayList)1 Set (java.util.Set)1 MockitoException (org.mockito.exceptions.base.MockitoException)1 InjectMocksScanner (org.mockito.internal.configuration.injection.scanner.InjectMocksScanner)1 MockScanner (org.mockito.internal.configuration.injection.scanner.MockScanner)1 Sets.newMockSafeHashSet (org.mockito.internal.util.collections.Sets.newMockSafeHashSet)1 MemberAccessor (org.mockito.plugins.MemberAccessor)1