private static Object spyInstance(Field field, Object instance) { return Mockito.mock(instance.getClass(), withSettings().spiedInstance(instance) .defaultAnswer(CALLS_REAL_METHODS) .name(field.getName())); }
private static <T> T spy(T subject) { return Mockito.mock( (Class<T>) subject.getClass(), VERIFIEABLE_MOCK_SETTINGS.spiedInstance( subject ) ); }
if (Modifier.isPrivate(constructor.getModifiers())) { constructor.setAccessible(true); return Mockito.mock(type, settings.spiedInstance(constructor.newInstance())); } else { return Mockito.mock(type, settings.useConstructor());
@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; } }
field.setAccessible(true); field.set(testInstance, Mockito.mock(instance.getClass(), withSettings() .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; } }
.spiedInstance(object) .defaultAnswer(CALLS_REAL_METHODS));
@SuppressWarnings( "unchecked" ) @Override public void dispatch( PendingReferenceCheck<T> reporter ) { reference.dispatch( mock( (Class<PendingReferenceCheck<T>>) reporter.getClass(), withSettings().spiedInstance( reporter ) .defaultAnswer( new ReporterSpy<>( reference, reporter, log ) ) ) ); } }
public static <T> T spy(T object) { return MOCKITO_CORE.mock((Class<T>) object.getClass(), withSettings() .spiedInstance(object) .defaultAnswer(CALLS_REAL_METHODS));
@SuppressWarnings({ "unchecked", "rawtypes" }) private <T> T createCollectionFromUnknownInterface(Class<T> collectionClass, Collection<?> contents) { final Collection<Object> spiedCollection; if (SortedSet.class.isAssignableFrom(collectionClass)) { spiedCollection = new LinkedSortedSet(); } else if (List.class.isAssignableFrom(collectionClass)) { spiedCollection = new ArrayList(getInitialCapacity(contents)); } else if (Queue.class.isAssignableFrom(collectionClass)) { spiedCollection = new ConcurrentLinkedQueue(); } else { spiedCollection = new OrderedSet(getInitialCapacity(contents)); } return (T) mock( spiedCollection.getClass(), withSettings().spiedInstance(spiedCollection).defaultAnswer(CALLS_REAL_METHODS) .extraInterfaces(collectionClass)); }
public <T> T spy(final T object) { MockSettings mockSettings = Mockito.withSettings() .spiedInstance(object) .defaultAnswer(POWER_MOCK_CALL_REAL_METHOD); //noinspection unchecked return DefaultMockCreator.mock((Class<T>) Whitebox.getType(object), false, true, object, mockSettings, (Method[]) null); }
field.setAccessible(true); field.set(testInstance, Mockito.mock(instance.getClass(), withSettings() .spiedInstance(instance) .defaultAnswer(Mockito.CALLS_REAL_METHODS) .name(field.getName())));
@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); }
.spiedInstance(object) .defaultAnswer(CALLS_REAL_METHODS));
@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; } }
@SuppressWarnings("unchecked") @Override public void dispatch( PendingReferenceCheck<T> reporter ) { reference.dispatch( mock( (Class<PendingReferenceCheck<T>>) reporter.getClass(), withSettings().spiedInstance( reporter ) .defaultAnswer( new ReporterSpy<>( reference, reporter, log ) ) ) ); } }