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); } else if (acrossJVMSerializationFeature.isWriteReplace(method)) { return acrossJVMSerializationFeature.writeReplace(proxy); } MockitoMethodProxy mockitoMethodProxy = createMockitoMethodProxy(methodProxy); new CGLIBHacker().setMockitoNamingPolicy(methodProxy); MockitoMethod mockitoMethod = createMockitoMethod(method); CleanTraceRealMethod realMethod = new CleanTraceRealMethod(mockitoMethodProxy); Invocation invocation = new InvocationImpl(proxy, mockitoMethod, args, SequenceNumber.next(), realMethod); return handler.handle(invocation); }
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); } else if (acrossJVMSerializationFeature.isWriteReplace(method)) { return acrossJVMSerializationFeature.writeReplace(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); }
/** * Build the invocation * * If the method was not specified, use IMethods methods. * * @return invocation */ public Invocation toInvocation() { if (method == null) { if (argTypes == null) { argTypes = new LinkedList<Class<?>>(); for (Object arg : args) { if (arg == null) { argTypes.add(Object.class); } else { argTypes.add(arg.getClass()); } } } try { method = MethodsImpl.class.getMethod(methodName, argTypes.toArray(new Class[argTypes.size()])); } catch (Exception e) { throw new RuntimeException("builder only creates invocations of IMethods interface", e); } } Invocation i = new InvocationImpl(mock, new SerializableMethod(method), args, sequenceNumber, null); if (verified) { i.markVerified(); } return i; }
@Test public void shouldProvideOwnImplementationOfHashCode() throws Throwable { //when Object ret = filter.intercept(new MethodsImpl(), MethodsImpl.class.getMethod("hashCode"), new Object[0], null); //then assertTrue((Integer) ret != 0); Mockito.verify(handler, never()).handle(any(InvocationImpl.class)); }
@Test public void shouldProvideOwnImplementationOfEquals() throws Throwable { //when MethodsImpl proxy = new MethodsImpl(); Object ret = filter.intercept(proxy, MethodsImpl.class.getMethod("equals", Object.class), new Object[] {proxy}, null); //then assertTrue((Boolean) ret); Mockito.verify(handler, never()).handle(any(InvocationImpl.class)); }
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { if (objectMethodsGuru.isEqualsMethod(method)) { return proxy == args[0]; } else if (objectMethodsGuru.isHashCodeMethod(method)) { return System.identityHashCode(proxy); } if (args == null) { throw new IllegalArgumentException(); } ProxiedMethod proxiedMethod = new ProxiedMethod(method); return handler.handle(new InvocationImpl(proxy, proxiedMethod, args, SequenceNumber.next(), proxiedMethod)); }
protected static Invocation invocationOf(Class<?> type, String methodName, Object ... args) throws NoSuchMethodException { Class[] types = new Class[args.length]; for (int i = 0; i < args.length; i++) { types[i] = args[i].getClass(); } return new InvocationImpl(mock(type), new SerializableMethod(type.getMethod(methodName, types)), args, 1, null); }
public MockitoMethodProxy build() { IMethods mock = mock(IMethods.class); when(mock.objectReturningMethodNoArgs()).thenAnswer(new Answer<Object>() { public Object answer(InvocationOnMock invocation) throws Throwable { return invocation; }}); InvocationImpl i = (InvocationImpl) mock.objectReturningMethodNoArgs(); return new ExposedInvocation(i).getMethodProxy(); }
protected static Invocation invocationOf(Class<?> type, String methodName, RealMethod realMethod) throws NoSuchMethodException { return new InvocationImpl(new Object(), new SerializableMethod(type.getMethod(methodName, new Class[0])), new Object[0], 1, realMethod); }
private void stubOrdinaryInvocationWithInvocationMatcher(MockHandlerImpl<?> handler, StubbedInvocationMatcher value) { handler.invocationContainerImpl = mock(InvocationContainerImpl.class); given(handler.invocationContainerImpl.findAnswerFor(any(InvocationImpl.class))).willReturn(value); }