public void resetMock(Object mock, MockHandler newHandler, MockCreationSettings settings) { ((Factory) mock).setCallback(0, new MethodInterceptorFilter(cast(newHandler), settings)); }
public <T> T createMock(MockCreationSettings<T> settings, MockHandler handler) { InternalMockHandler mockitoHandler = cast(handler); return ClassImposterizer.INSTANCE.imposterise( new MethodInterceptorFilter(mockitoHandler, settings), settings.getTypeToMock(), settings.getExtraInterfaces()); }
/** * Enhance the mock class instance with mocking ability. * * @param mockToEnhance * the mock to enhance */ void enhance(T mockToEnhance) { MockUtil mockUtil = getMockUtil(); MockitoInvocationHandler mockHandler = (MockitoInvocationHandler) mockUtil.getMockHandler(mock); MethodInterceptorFilter filter = new MethodInterceptorFilter(mockHandler, getDefaultSettings()); ((Factory) mockToEnhance).setCallbacks(new Callback[] { filter, SerializableNoOp.SERIALIZABLE_INSTANCE }); }
public <T> void resetMock(T mock) { MockHandlerInterface<T> oldMockHandler = getMockHandler(mock); MockHandler<T> newMockHandler = new MockHandler<T>(oldMockHandler); MethodInterceptorFilter newFilter = new MethodInterceptorFilter(newMockHandler, (MockSettingsImpl) withSettings().defaultAnswer(RETURNS_DEFAULTS)); ((Factory) mock).setCallback(0, newFilter); }
public <T> T createMock(Class<T> classToMock, MockSettingsImpl settings) { creationValidator.validateType(classToMock); creationValidator.validateExtraInterfaces(classToMock, settings.getExtraInterfaces()); creationValidator.validateMockedType(classToMock, settings.getSpiedInstance()); settings.initiateMockName(classToMock); MockHandler<T> mockHandler = new MockHandler<T>(settings); InvocationNotifierHandler<T> invocationNotifierHandler = new InvocationNotifierHandler<T>(mockHandler, settings); MethodInterceptorFilter filter = new MethodInterceptorFilter(invocationNotifierHandler, settings); Class<?>[] interfaces = settings.getExtraInterfaces(); Class<?>[] ancillaryTypes; if (settings.isSerializable()) { ancillaryTypes = interfaces == null ? new Class<?>[] {Serializable.class} : new ArrayUtils().concat(interfaces, Serializable.class); } else { ancillaryTypes = interfaces == null ? new Class<?>[0] : interfaces; } Object spiedInstance = settings.getSpiedInstance(); T mock = ClassImposterizer.INSTANCE.imposterise(filter, classToMock, ancillaryTypes); if (spiedInstance != null) { new LenientCopyTool().copyToMock(spiedInstance, mock); } return mock; }