private static Object spyInstance(Field field, Object instance) { return Mockito.mock(instance.getClass(), withSettings().spiedInstance(instance) .defaultAnswer(CALLS_REAL_METHODS) .name(field.getName())); }
/** * Specifies mock name. Naming mocks can be helpful for debugging - the name is used in all verification errors. * <p> * Beware that naming mocks is not a solution for complex code which uses too many mocks or collaborators. * <b>If you have too many mocks then refactor the code</b> so that it's easy to test/debug without necessity of naming mocks. * <p> * <b>If you use <code>@Mock</code> annotation then you've got naming mocks for free!</b> <code>@Mock</code> uses field name as mock name. {@link Mock Read more.} * <p> * * See examples in javadoc for {@link Mockito} class * * @param classToMock class or interface to mock * @param name of the mock * @return mock object */ public static <T> T mock(Class<T> classToMock, String name) { return mock(classToMock, withSettings() .name(name) .defaultAnswer(RETURNS_DEFAULTS)); }
/** * Specifies mock name. Naming mocks can be helpful for debugging - the name is used in all verification errors. * <p> * Beware that naming mocks is not a solution for complex code which uses too many mocks or collaborators. * <b>If you have too many mocks then refactor the code</b> so that it's easy to test/debug without necessity of naming mocks. * <p> * <b>If you use <code>@Mock</code> annotation then you've got naming mocks for free!</b> <code>@Mock</code> uses field name as mock name. {@link Mock Read more.} * <p> * * See examples in javadoc for {@link Mockito} class * * @param classToMock class or interface to mock * @param name of the mock * @return mock object */ @CheckReturnValue public static <T> T mock(Class<T> classToMock, String name) { return mock(classToMock, withSettings() .name(name) .defaultAnswer(RETURNS_DEFAULTS)); }
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); } }
private static Object spyNewInstance(Object testInstance, Field field) throws InstantiationException, IllegalAccessException, InvocationTargetException { MockSettings settings = withSettings().defaultAnswer(CALLS_REAL_METHODS) .name(field.getName()); Class<?> type = field.getType(); if (type.isInterface()) {
@Override protected boolean processInjection(Field field, Object fieldOwner, Set<Object> mockCandidates) { FieldReader fieldReader = new FieldReader(fieldOwner, field); // TODO refoctor : code duplicated in SpyAnnotationEngine if(!fieldReader.isNull() && field.isAnnotationPresent(Spy.class)) { try { Object instance = fieldReader.read(); if (MockUtil.isMock(instance)) { // A. instance has been spied earlier // B. protect against multiple use of MockitoAnnotations.initMocks() Mockito.reset(instance); } else { Object mock = Mockito.mock(instance.getClass(), withSettings() .spiedInstance(instance) .defaultAnswer(Mockito.CALLS_REAL_METHODS) .name(field.getName())); setField(fieldOwner, field, mock); } } catch (Exception e) { throw new MockitoException("Problems initiating spied field " + field.getName(), e); } } return false; } }
.spiedInstance(instance) .defaultAnswer(Mockito.CALLS_REAL_METHODS) .name(field.getName())));
@Override protected boolean processInjection(Field field, Object fieldOwner, Set<Object> mockCandidates) { FieldReader fieldReader = new FieldReader(fieldOwner, field); // TODO refoctor : code duplicated in SpyAnnotationEngine if(!fieldReader.isNull() && field.isAnnotationPresent(Spy.class)) { try { Object instance = fieldReader.read(); if (new MockUtil().isMock(instance)) { // A. instance has been spied earlier // B. protect against multiple use of MockitoAnnotations.initMocks() Mockito.reset(instance); } else { new FieldSetter(fieldOwner, field).set( Mockito.mock(instance.getClass(), withSettings() .spiedInstance(instance) .defaultAnswer(Mockito.CALLS_REAL_METHODS) .name(field.getName())) ); } } catch (Exception e) { throw new MockitoException("Problems initiating spied field " + field.getName(), e); } } return false; } }
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); } }
@SuppressWarnings("unchecked") private MockCreationSettings<Class> createStaticMockSettings(final Class mock) { return Mockito.withSettings() .name(mock.getName()) .build((Class<Class>) mock); }
/** * Specifies mock name. Naming mocks can be helpful for debugging - the name is used in all verification errors. * <p> * Beware that naming mocks is not a solution for complex code which uses too many mocks or collaborators. * <b>If you have too many mocks then refactor the code</b> so that it's easy to test/debug without necessity of naming mocks. * <p> * <b>If you use @Mock annotation then you've got naming mocks for free!</b> @Mock uses field name as mock name. {@link Mock Read more.} * <p> * * See examples in javadoc for {@link Mockito} class * * @param classToMock class or interface to mock * @param name of the mock * @return mock object */ public static <T> T mock(Class<T> classToMock, String name) { return mock(classToMock, withSettings() .name(name) .defaultAnswer(RETURNS_DEFAULTS)); }
private Object createMock(Object testInstance, Field field) { try { Object instance = field.get(testInstance); if (MockUtil.isMock(instance)) { // instance has been spied earlier // for example happens when MockitoAnnotations.initMocks is called two times. Mockito.reset(instance); return instance; } else { return Mockito.mock( GwtMockitoUtils.getTypeToMock(field), withSettings().name(field.getName()) ); } } catch (IllegalAccessException e) { throw new MockitoException("Problems initiating spied field " + field.getName(), e); } } }
private Object createMock(Object testInstance, Field field) { try { Object instance = field.get(testInstance); if (MockUtil.isMock(instance)) { // instance has been spied earlier // for example happens when MockitoAnnotations.initMocks is called two times. Mockito.reset(instance); return instance; } else { return Mockito.mock( GwtMockitoUtils.getTypeToMock(field), withSettings().name(field.getName()) ); } } catch (IllegalAccessException e) { throw new MockitoException("Problems initiating spied field " + field.getName(), e); } } }
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); }
@SuppressWarnings("unchecked") public <T> T createMock(String name) { MockSettings settings = MockReset.withSettings(getReset()); if (StringUtils.hasLength(name)) { settings.name(name); } if (!this.extraInterfaces.isEmpty()) { settings.extraInterfaces(ClassUtils.toClassArray(this.extraInterfaces)); } settings.defaultAnswer(this.answer); if (this.serializable) { settings.serializable(); } return (T) Mockito.mock(this.typeToMock.resolve(), settings); }
@SuppressWarnings("unchecked") public <T> T createSpy(String name, Object instance) { Assert.notNull(instance, "Instance must not be null"); Assert.isInstanceOf(this.typeToSpy.resolve(), instance); if (Mockito.mockingDetails(instance).isSpy()) { return (T) instance; } MockSettings settings = MockReset.withSettings(getReset()); if (StringUtils.hasLength(name)) { settings.name(name); } settings.spiedInstance(instance); settings.defaultAnswer(Mockito.CALLS_REAL_METHODS); if (this.isProxyTargetAware()) { settings.verificationStartedListeners( new SpringAopBypassingVerificationStartedListener()); } return (T) Mockito.mock(instance.getClass(), 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)); } } }
@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 MockSettings createMockSettings() { MockSettings mockSettings = new MockSettingsImpl(); if (extraInterfaces != null && extraInterfaces.length > 0) { mockSettings.extraInterfaces(extraInterfaces); } if (defaultAnswer != null) { mockSettings.defaultAnswer(defaultAnswer.get()); } else { mockSettings.defaultAnswer(Answers.RETURNS_DEFAULTS.get()); } if (mockName != null) { mockSettings.name(mockName); } return mockSettings; }