public void resetMock(Object mock, MockHandler newHandler, MockCreationSettings settings) { ((Factory) mock).setCallback(0, new MethodInterceptorFilter(cast(newHandler), settings)); }
public MockHandler getHandler(Object mock) { if (!(mock instanceof Factory)) { return null; } Factory factory = (Factory) mock; Callback callback = factory.getCallback(0); if (!(callback instanceof MethodInterceptorFilter)) { return null; } return ((MethodInterceptorFilter) callback).getHandler(); } }
public Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy) throws Throwable { if (objectMethodsGuru.isEqualsMethod(method)) { return proxy == args[0]; } else if (objectMethodsGuru.isHashCodeMethod(method)) { return hashCodeForMock(proxy); } MockitoMethodProxy mockitoMethodProxy = createMockitoMethodProxy(methodProxy); cglibHacker.setMockitoNamingPolicy(mockitoMethodProxy); MockitoMethod mockitoMethod = createMockitoMethod(method); FilteredCGLIBProxyRealMethod realMethod = new FilteredCGLIBProxyRealMethod(mockitoMethodProxy); Invocation invocation = new InvocationImpl(proxy, mockitoMethod, args, SequenceNumber.next(), realMethod); return handler.handle(invocation); }
@Override public synchronized Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy) throws Throwable { return super.intercept(proxy, method, args, methodProxy); } }
@Override public Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy) throws Throwable { if (objectMethodsGuru.isGetMetaClass(method)) { return GroovySystem.getMetaClassRegistry().getMetaClass(typeToMock); } return super.intercept(proxy, method, args, methodProxy); } }
public Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy) throws Throwable { if (objectMethodsGuru.isEqualsMethod(method)) { return proxy == args[0]; } else if (objectMethodsGuru.isHashCodeMethod(method)) { return hashCodeForMock(proxy); } MockitoMethodProxy mockitoMethodProxy = createMockitoMethodProxy(methodProxy); cglibHacker.setMockitoNamingPolicy(mockitoMethodProxy); MockitoMethod mockitoMethod = createMockitoMethod(method); FilteredCGLIBProxyRealMethod realMethod = new FilteredCGLIBProxyRealMethod(mockitoMethodProxy); Invocation invocation = new Invocation(proxy, mockitoMethod, args, SequenceNumber.next(), realMethod); return handler.handle(invocation); }
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()); }
public MockHandler getHandler(Object mock) { if (!(mock instanceof Factory)) { return null; } Factory factory = (Factory) mock; Callback callback = factory.getCallback(0); if (!(callback instanceof MethodInterceptorFilter)) { return null; } return ((MethodInterceptorFilter) callback).getHandler(); } }
/** * 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> MockHandlerInterface<T> getMockHandler(T mock) { if (mock == null) { throw new NotAMockException("Argument should be a mock, but is null!"); } if (isMockitoMock(mock)) { return (MockHandlerInterface) getInterceptor(mock).getHandler(); } else { throw new NotAMockException("Argument should be a mock, but is: " + mock.getClass()); } }
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; }