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); } 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 void setMockitoNamingPolicy(MockitoMethodProxy mockitoMethodProxy) { try { MethodProxy methodProxy = mockitoMethodProxy.getMethodProxy(); Field createInfoField = reflectOnCreateInfo(methodProxy); createInfoField.setAccessible(true); Object createInfo = createInfoField.get(methodProxy); Field namingPolicyField = createInfo.getClass().getDeclaredField("namingPolicy"); namingPolicyField.setAccessible(true); if (namingPolicyField.get(createInfo) == null) { namingPolicyField.set(createInfo, MockitoNamingPolicy.INSTANCE); } } catch (Exception e) { throw new RuntimeException( "Unable to set MockitoNamingPolicy on cglib generator which creates FastClasses", e); } }
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); }
public void setMockitoNamingPolicy(MockitoMethodProxy mockitoMethodProxy) { try { MethodProxy methodProxy = mockitoMethodProxy.getMethodProxy(); final Field createInfoField = reflectOnCreateInfo(methodProxy); AccessController.doPrivileged(new PrivilegedAction<Void>() { @Override public Void run() { createInfoField.setAccessible(true); return null; } }); Object createInfo = createInfoField.get(methodProxy); final Field namingPolicyField = createInfo.getClass().getDeclaredField("namingPolicy"); AccessController.doPrivileged(new PrivilegedAction<Void>() { @Override public Void run() { namingPolicyField.setAccessible(true); return null; } }); if (namingPolicyField.get(createInfo) == null) { namingPolicyField.set(createInfo, MockitoNamingPolicy.INSTANCE); } } catch (Exception e) { throw new RuntimeException( "Unable to set MockitoNamingPolicy on cglib generator which creates FastClasses", e); } }
@SuppressWarnings("unchecked") private Field reflectOnCreateInfo(MethodProxy methodProxy) throws SecurityException, NoSuchFieldException { Class cglibMethodProxyClass = methodProxy.getClass(); // in case methodProxy was extended by user, let's traverse the object // graph to find the cglib methodProxy // with all the fields we would like to change while (cglibMethodProxyClass != MethodProxy.class) { cglibMethodProxyClass = methodProxy.getClass().getSuperclass(); } return cglibMethodProxyClass.getDeclaredField("createInfo"); }
public void setMockitoNamingPolicy(MethodProxy methodProxy) { try { Field createInfoField = reflectOnCreateInfo(methodProxy); createInfoField.setAccessible(true); Object createInfo = createInfoField.get(methodProxy); Field namingPolicyField = createInfo.getClass().getDeclaredField("namingPolicy"); namingPolicyField.setAccessible(true); if (namingPolicyField.get(createInfo) == null) { namingPolicyField.set(createInfo, MockitoNamingPolicy.INSTANCE); } } catch (Exception e) { throw new RuntimeException( "Unable to set MockitoNamingPolicy on cglib generator which creates FastClasses", e); } }
public SerializableMockitoMethodProxy(MethodProxy methodProxy) { assert methodProxy != null; Object info = Whitebox.getInternalState(methodProxy, "createInfo"); c1 = (Class<?>) Whitebox.getInternalState(info, "c1"); c2 = (Class<?>) Whitebox.getInternalState(info, "c2"); desc = methodProxy.getSignature().getDescriptor(); name = methodProxy.getSignature().getName(); superName = methodProxy.getSuperName(); this.methodProxy = methodProxy; }
@Test public void shouldCreateSerializableMethodProxyIfIsSerializableMock() throws Exception { MethodInterceptorFilter filter = new MethodInterceptorFilter(handler, (MockSettingsImpl) withSettings().serializable()); MethodProxy methodProxy = MethodProxy.create(String.class, String.class, "", "toString", "toString"); // when MockitoMethodProxy mockitoMethodProxy = filter.createMockitoMethodProxy(methodProxy); // then assertThat(mockitoMethodProxy, instanceOf(SerializableMockitoMethodProxy.class)); }
@Test public void shouldCreateNONSerializableMethodProxyIfIsNotSerializableMock() throws Exception { MethodInterceptorFilter filter = new MethodInterceptorFilter(handler, (MockSettingsImpl) withSettings()); MethodProxy methodProxy = MethodProxy.create(String.class, String.class, "", "toString", "toString"); // when MockitoMethodProxy mockitoMethodProxy = filter.createMockitoMethodProxy(methodProxy); // then assertThat(mockitoMethodProxy, instanceOf(DelegatingMockitoMethodProxy.class)); }
public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable { if (new ObjectMethodsGuru().isToString(method)) { return "SmartNull returned by this unstubbed method call on a mock:\n" + invocation.toString(); } new Reporter().smartNullPointerException(invocation.toString(), location); return null; }
public SerializableMockitoMethodProxy(MethodProxy methodProxy) { Object info = Whitebox.getInternalState(methodProxy, "createInfo"); c1 = (Class<?>) Whitebox.getInternalState(info, "c1"); c2 = (Class<?>) Whitebox.getInternalState(info, "c2"); desc = methodProxy.getSignature().getDescriptor(); name = methodProxy.getSignature().getName(); superName = methodProxy.getSuperName(); this.methodProxy = methodProxy; }
@Test public void shouldCreateCorrectCreationInfo() throws Exception { // given MethodProxy proxy = MethodProxy.create(String.class, Integer.class, "", "", ""); SerializableMockitoMethodProxy serializableMockitoMethodProxy = new SerializableMockitoMethodProxy(proxy); // when Object methodProxy = Whitebox.invokeMethod(serializableMockitoMethodProxy, "getMethodProxy", new Object[0]); // then Object info = Whitebox.getInternalState(methodProxy, "createInfo"); assertEquals(String.class, Whitebox.getInternalState(info, "c1")); assertEquals(Integer.class, Whitebox.getInternalState(info, "c2")); }
@Test public void shouldCreateCorrectSignatures() throws Exception { // given MethodProxy proxy = MethodProxy.create(String.class, Integer.class, "a", "b", "c"); SerializableMockitoMethodProxy serializableMockitoMethodProxy = new SerializableMockitoMethodProxy(proxy); // when MethodProxy methodProxy = (MethodProxy) Whitebox.invokeMethod(serializableMockitoMethodProxy, "getMethodProxy", new Object[0]); // then assertEquals("a", methodProxy.getSignature().getDescriptor()); assertEquals("b", methodProxy.getSignature().getName()); assertEquals("c", methodProxy.getSuperName()); }
public MockitoMethodProxy createMockitoMethodProxy(MethodProxy methodProxy) { if (mockSettings.isSerializable()) return new SerializableMockitoMethodProxy(methodProxy); return new DelegatingMockitoMethodProxy(methodProxy); }
public DelegatingMockitoMethodProxy(MethodProxy methodProxy) { this.methodProxy = methodProxy; }
MethodProxy getMethodProxy() { if (methodProxy == null) { methodProxy = MethodProxy.create(c1, c2, desc, name, superName); } return methodProxy; }
public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable { return null; }
public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable { return methodProxy.invokeSuper(o, objects); }
public MethodProxy getMethodProxy() { return methodProxy; }
public MethodProxy getMethodProxy() { if (methodProxy == null) methodProxy = MethodProxy.create(c1, c2, desc, name, superName); return methodProxy; }