Search in sources :

Example 1 with ForwardingObject

use of com.google.common.collect.ForwardingObject in project guava by google.

the class ForwardingObjectTester method testForwardingObject.

/**
   * Ensures that all interface methods of {@code forwarderClass} are forwarded to the
   * {@link ForwardingObject#delegate}. {@code forwarderClass} is assumed to only implement one
   * interface.
   */
static <T extends ForwardingObject> void testForwardingObject(final Class<T> forwarderClass) {
    // super interface type of T
    @SuppressWarnings("unchecked") Class<? super T> interfaceType = (Class<? super T>) Iterables.getOnlyElement(Arrays.asList(forwarderClass.getInterfaces()));
    new ForwardingWrapperTester().testForwarding(interfaceType, new Function<Object, T>() {

        @Override
        public T apply(Object delegate) {
            T mock = mock(forwarderClass, CALLS_REAL_METHODS.get());
            try {
                T stubber = doReturn(delegate).when(mock);
                DELEGATE_METHOD.invoke(stubber);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            return mock;
        }
    });
}
Also used : ForwardingWrapperTester(com.google.common.testing.ForwardingWrapperTester) ForwardingObject(com.google.common.collect.ForwardingObject)

Aggregations

ForwardingObject (com.google.common.collect.ForwardingObject)1 ForwardingWrapperTester (com.google.common.testing.ForwardingWrapperTester)1