Search in sources :

Example 1 with Answers

use of org.mockito.Answers in project powermock by powermock.

the class AnnotationEnabler method standardInject.

private void standardInject(Object testInstance) throws IllegalAccessException {
    Set<Field> fields = Whitebox.getFieldsAnnotatedWith(testInstance, getMockAnnotations());
    for (Field field : fields) {
        if (field.get(testInstance) != null) {
            continue;
        }
        final Class<?> type = field.getType();
        if (field.isAnnotationPresent(org.powermock.core.classloader.annotations.Mock.class)) {
            org.powermock.core.classloader.annotations.Mock annotation = field.getAnnotation(org.powermock.core.classloader.annotations.Mock.class);
            final String[] value = annotation.value();
            if (value.length != 1 || !"".equals(value[0])) {
                System.err.println("PowerMockito deprecation: Use PowerMockito.spy(..) for partial mocking instead. A standard mock will be created instead.");
            }
        }
        if (field.isAnnotationPresent(org.mockito.Mock.class)) {
            org.mockito.Mock mockAnnotation = field.getAnnotation(org.mockito.Mock.class);
            MockSettings mockSettings = withSettings();
            Answers answers = mockAnnotation.answer();
            if (answers != null) {
                mockSettings.defaultAnswer(answers.get());
            }
            Class<?>[] extraInterfaces = mockAnnotation.extraInterfaces();
            if (extraInterfaces != null && extraInterfaces.length > 0) {
                mockSettings.extraInterfaces(extraInterfaces);
            }
            String name = mockAnnotation.name();
            if (name != null && name.length() > 0) {
                mockSettings.name(name);
            }
            field.set(testInstance, mock(type, mockSettings));
        } else {
            field.set(testInstance, mock(type));
        }
    }
}
Also used : Answers(org.mockito.Answers) Mock(org.mockito.Mock) Field(java.lang.reflect.Field) MockSettings(org.mockito.MockSettings)

Aggregations

Field (java.lang.reflect.Field)1 Answers (org.mockito.Answers)1 Mock (org.mockito.Mock)1 MockSettings (org.mockito.MockSettings)1