/** * 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) { @SuppressWarnings("unchecked") // super interface type of T 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; } }); }
@Override public Object apply(final Object input) { return new ForwardingObject() { @Override protected Object delegate() { return input; } @Override public int hashCode() { return Objects2.hashCodes(this.delegate()); } @Override public boolean equals(Object obj) { return Objects2.isEqual(this.delegate(), obj); } }; }
/** * 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) { @SuppressWarnings("unchecked") // super interface type of T 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 = EasyMock.createMockBuilder(forwarderClass) .addMockedMethod(DELEGATE_METHOD) .createMock(); try { DELEGATE_METHOD.invoke(mock); } catch (Exception e) { throw Throwables.propagate(e); } EasyMock.expectLastCall().andStubReturn(delegate); EasyMock.replay(mock); return mock; } }); }
/** * 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) { @SuppressWarnings("unchecked") // super interface type of T 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; } }); }
@Override public Object apply(final Object input) { return new ForwardingObject() { @Override protected Object delegate() { return input; } @Override public String toString() { return Objects2.toString(this.delegate()); } }; }