All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.kubek2k.springockito.annotations.DesiredMockitoBeansFinder Maven / Gradle / Ivy

There is a newer version: 1.0.9
Show newest version
package org.kubek2k.springockito.annotations;

import sun.reflect.annotation.AnnotationType;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

class DesiredMockitoBeansFinder {
    public static class MockProperties {
        private AnnotationType annotationValues;

        private Class mockClass;

        public MockProperties(AnnotationType annotationValues, Class mockClass) {
            this.annotationValues = annotationValues;
            this.mockClass = mockClass;
        }

        public AnnotationType getAnnotationValues() {
            return annotationValues;
        }

        public Class getMockClass() {
            return mockClass;
        }
    }

    public Map> findMockedBeans(Class clazz) {
        return findAnnotatedFieldsTypes(clazz.getDeclaredFields(), ReplaceWithMock.class);
    }

    public Set findSpiedBeans(Class clazz) {
        return findAnnotatedFieldsTypes(clazz.getDeclaredFields(), WrapWithSpy.class).keySet();
    }

    private  Map> findAnnotatedFieldsTypes(Field[] fieldsToScan, Class annotationClass) {
        Map> mockedBeans = new HashMap>();
        for (Field field : fieldsToScan) {
            Annotation replaceWithMockAnnotation = field.getAnnotation(annotationClass);
            if (replaceWithMockAnnotation != null) {
                mockedBeans.put(field.getName(), new MockProperties((AnnotationType)field.getAnnotation(annotationClass), field.getType()));
            }
        }
        return mockedBeans;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy