### Question:
CachingObjectFactory { public static <I, C extends I> I newCachingObject(Class<I> interfaceClass, C delegate) { return (I) Proxy.newProxyInstance( CachingObjectFactory.class.getClassLoader(), new Class<?>[]{interfaceClass}, new CachingInvocationHandler(interfaceClass, delegate) ); }  static I newCachingObject(Class<I> interfaceClass, C delegate);  }### Answer:
@Test public void calls_delegate_once_also_with_two_calls() { MyInterface cachingObject = CachingObjectFactory.newCachingObject(MyInterface.class, implementation); assertEquals("first call", VALUE, cachingObject.value()); assertEquals("second call", VALUE, cachingObject.value()); assertEquals(1, implementation.called); }
@Test public void differentiate_on_parameters() { MyInterface cachingObject = CachingObjectFactory.newCachingObject(MyInterface.class, implementation); assertEquals("first call", VALUE, cachingObject.parametrized(1, "hello", new ComplexParam(2))); assertEquals("second call with different params", VALUE, cachingObject.parametrized(2, "hello", new ComplexParam(2))); assertEquals(2, implementation.called); }
@Test public void handle_nulls() { MyInterface cachingObject = CachingObjectFactory.newCachingObject(MyInterface.class, implementation); assertEquals("first call", VALUE, cachingObject.parametrized(new Integer(1), null, new ComplexParam(2))); assertEquals("second call", VALUE, cachingObject.parametrized(1, null, new ComplexParam(2))); assertEquals(1, implementation.called); }