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); } }
String name = mockAnnotation.name(); if (name != null && name.length() > 0) { mockSettings.name(name);
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 String getMockName(Parameter parameter) { String explicitMockName = parameter.getAnnotation(Mock.class).name().trim(); if (!explicitMockName.isEmpty()) { return explicitMockName; } else if (parameter.isNamePresent()) { return parameter.getName(); } return null; }
private static void initMockito(Object junitTest) { MockitoAnnotations.initMocks(junitTest); Class<?> clazz = junitTest.getClass(); Field[] fields = clazz.getDeclaredFields(); for (int i=0; i<fields.length; i++ ) { final Mock annotation = fields[i].getAnnotation(Mock.class); final boolean isAnnotated = annotation != null; final String classNameContains = "Mockito"; try { Object value = fields[i].get(junitTest); if (value == null && isAnnotated) { throw new RuntimeException("Could not retrieve mock object bound to field '" + fields[i].getName() + "' of test class '" + clazz.getSimpleName() + "'. Value is null."); } else if (value == null) { log.info("Could not inspect object bound to field '" + fields[i].getName() + "' of test class '" + clazz.getSimpleName() + "'. Value is null."); } else if (isAnnotated || value.getClass().getName().contains(classNameContains)) { String key = isAnnotated && annotation.name() != null && !annotation.name().equals("") ? annotation.name() : fields[i].getName(); log.info("Due to " + (isAnnotated ? "Annotation" : "class name containing expected string '" + classNameContains + "'") + " registered object bound to field '" + key + "' of test class '" + clazz.getSimpleName() + "' as mock associated with key '" + key + "'."); Mocks.register(key, value); } else { log.info("Did not before object bound to field '" + fields[i].getName() + "' of test class '" + clazz.getSimpleName() + "' as mock. Class name does not contain the expected string '" + classNameContains + "'."); } } catch (IllegalAccessException e) { if (isAnnotated) { throw new RuntimeException("Could not retrieve mock object bound to field '" + fields[i].getName() + "' of test class '" + clazz.getSimpleName() + "'. Field must be declared as public.", e); } log.warning("Could not inspect object bound to field '" + fields[i].getName() + "' of test class '" + clazz.getSimpleName() + "'. Field is not accessible."); } } }
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 String getMockName(Parameter parameter) { String explicitMockName = parameter.getAnnotation(Mock.class).name().trim(); if (!explicitMockName.isEmpty()) { return explicitMockName; } else if (parameter.isNamePresent()) { return parameter.getName(); } return null; }
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)); } } }