NullObject implements InvocationHandler { @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { final T target = get(); if (target == null) { return null; } else { if (!method.isAccessible()) { method.setAccessible(true); } return method.invoke(target, args); } } NullObject(@Nullable T target); @Override Object invoke(Object proxy, Method method, Object[] args);  }
@Test public void shouldInvokeAccessibleTargetMethods() throws Throwable { final TestViewCallbacks spy = spy(target); final Method doJob = TestViewCallbacks.class.getMethod("doJob"); doJob.setAccessible(true); final NullObject<TestViewCallbacks> nullObject = new NullObject<>(spy); nullObject.invoke(null, doJob, new Object[0]); verify(spy).doJob(); }
@Test public void shouldInvokeInaccessibleTargetMethods() throws Throwable { final TestViewCallbacks spy = spy(target); final Method doJob = TestViewCallbacks.class.getMethod("doJob"); doJob.setAccessible(false); final NullObject<TestViewCallbacks> nullObject = new NullObject<>(spy); nullObject.invoke(null, doJob, new Object[0]); verify(spy).doJob(); }