DelegatedListenableExecutor extends ForwardingListeningExecutorService { @Override public <T> ListenableFuture<T> submit( final Callable<T> callable ) { if ( callable instanceof IListenableFutureDelegator ) { final ListenableFuture<T> delegateFuture = delegateExecutor.submit( callable ); return ( (IListenableFutureDelegator) callable ).delegate( delegateFuture ); } else { return delegateExecutor.submit( callable ); } } DelegatedListenableExecutor( final ExecutorService delegateExecutor ); @Override ListenableFuture<T> submit( final Callable<T> callable );  }
@Test public void submit() throws Exception { final ExecutorService exec = mock( ExecutorService.class ); final DelegatedListenableExecutor delegatedListenableExecutor = new DelegatedListenableExecutor( exec ); final Callable callable = mock( Callable.class ); delegatedListenableExecutor.submit( callable ); verify( exec, times( 1 ) ).execute( any( Runnable.class ) ); final AbstractAsyncReportExecution delegator = mock( AbstractAsyncReportExecution.class ); delegatedListenableExecutor.submit( delegator ); verify( exec, times( 2 ) ).execute( any( Runnable.class ) ); verify( delegator, times( 1 ) ).delegate( any( ListenableFuture.class ) ); }