public Object process(Mock annotation, Field field) { MockSettings mockSettings = Mockito.withSettings(); if (annotation.extraInterfaces().length > 0) { // never null mockSettings.extraInterfaces(annotation.extraInterfaces()); } if ("".equals(annotation.name())) { mockSettings.name(field.getName()); } else { mockSettings.name(annotation.name()); } // see @Mock answer default value mockSettings.defaultAnswer(annotation.answer().get()); return Mockito.mock(field.getType(), mockSettings); } }
Class<?>[] extraInterfaces = mockAnnotation.extraInterfaces(); if (extraInterfaces != null && extraInterfaces.length > 0) { mockSettings.extraInterfaces(extraInterfaces);
public static Object processAnnotationForMock(Mock annotation, Class<?> type, String name) { MockSettings mockSettings = Mockito.withSettings(); if (annotation.extraInterfaces().length > 0) { // never null mockSettings.extraInterfaces(annotation.extraInterfaces()); } if ("".equals(annotation.name())) { mockSettings.name(name); } else { mockSettings.name(annotation.name()); } if(annotation.serializable()){ mockSettings.serializable(); } if(annotation.stubOnly()){ mockSettings.stubOnly(); } if(annotation.lenient()){ mockSettings.lenient(); } // see @Mock answer default value mockSettings.defaultAnswer(annotation.answer()); return Mockito.mock(type, mockSettings); } }
private Object processAnnotationOn(Mock annotation, Field field) { MockSettings mockSettings = Mockito.withSettings(); if (annotation.extraInterfaces().length > 0) { // never null mockSettings.extraInterfaces(annotation.extraInterfaces()); } if ("".equals(annotation.name())) { mockSettings.name(field.getName()); } else { mockSettings.name(annotation.name()); } // see @Mock answer default value mockSettings.defaultAnswer(annotation.answer().get()); return Mockito.mock(field.getType(), mockSettings); }
@Override public Object getParameterValue(Parameter parameter) { Mock annotation = parameter.getAnnotation(Mock.class); MockSettings settings = Mockito.withSettings(); if (annotation.extraInterfaces().length > 0) { settings.extraInterfaces(annotation.extraInterfaces()); } if (annotation.serializable()) { settings.serializable(); } settings.name(annotation.name().isEmpty() ? parameter.getName() : annotation.name()); settings.defaultAnswer(annotation.answer()); return Mockito.mock(parameter.getType(), settings); } }
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.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)); } } }