ProxyHelper { @SuppressWarnings("unchecked") public static <T> T createProxyOf(T object, Object interceptor) { return (T) createProxyOf(object.getClass(), interceptor); }  @SuppressWarnings("unchecked") static T createProxyOf(T object, Object interceptor); @SuppressWarnings("unchecked") static T createProxyOf(Class<T>    clazz,
                                      Interceptor interceptor);  }
@Test public void shouldProxyMethodWithVarArgs() throws Throwable { ProxyHelper.Interceptor interceptor = mock(ProxyHelper.Interceptor.class); Stub stub = ProxyHelper.createProxyOf(Stub.class, interceptor); Method method = Stub.class.getMethod("testMethod", int.class, String[].class); when(interceptor.intercept(any(Method.class), nullable(Object[].class))).thenReturn(19); assertThat(stub.testMethod(1, "a", "b"), is(19)); verify(interceptor).intercept(eq(method), eq(new Object[]{1, new String[]{"a", "b"}})); }
@Test public void shouldProxyMethodWithOneVarArgsArg() throws Throwable { ProxyHelper.Interceptor interceptor = mock(ProxyHelper.Interceptor.class); Stub stub = ProxyHelper.createProxyOf(Stub.class, interceptor); Method method = Stub.class.getMethod("testMethod", int.class, String[].class); when(interceptor.intercept(any(Method.class), nullable(Object[].class))).thenReturn(19); assertThat(stub.testMethod(1, "a"), is(19)); verify(interceptor).intercept(eq(method), eq(new Object[]{1, new String[]{"a"}})); }
@Test public void shouldProxyMethodWithZeroVarArgsArgs() throws Throwable { ProxyHelper.Interceptor interceptor = mock(ProxyHelper.Interceptor.class); Stub stub = ProxyHelper.createProxyOf(Stub.class, interceptor); Method method = Stub.class.getMethod("testMethod", int.class, String[].class); when(interceptor.intercept(any(Method.class), nullable(Object[].class))).thenReturn(19); assertThat(stub.testMethod(1), is(19)); verify(interceptor).intercept(eq(method), eq(new Object[]{1, new String[0]})); }
@Test public void shouldProxyMethodWithOneNullVarArgsArg() throws Throwable { ProxyHelper.Interceptor interceptor = mock(ProxyHelper.Interceptor.class); Stub stub = ProxyHelper.createProxyOf(Stub.class, interceptor); Method method = Stub.class.getMethod("testMethod", int.class, String[].class); when(interceptor.intercept(any(Method.class), nullable(Object[].class))).thenReturn(19); assertThat(stub.testMethod(1, (String) null), is(19)); verify(interceptor).intercept(eq(method), eq(new Object[]{1, new String[]{null}})); }
@Test public void shouldProxyMethodWithNullVarArgs() throws Throwable { ProxyHelper.Interceptor interceptor = mock(ProxyHelper.Interceptor.class); Stub stub = ProxyHelper.createProxyOf(Stub.class, interceptor); Method method = Stub.class.getMethod("testMethod", int.class, String[].class); when(interceptor.intercept(any(Method.class), nullable(Object[].class))).thenReturn(19); assertThat(stub.testMethod(1, (String[]) null), is(19)); verify(interceptor).intercept(eq(method), eq(new Object[]{1, new String[]{null}})); }