### Question:
ContextExecutorService { protected void attachToContext(final UUID contextUuid, final Future<?> future) { final List<Future<?>> futures = getFuturesOfContext(contextUuid); futures.add(future); if(log.isDebugEnabled())log.debug("Task is attached to context " + contextUuid); executor.submit(new Runnable() { @Override public void run() { try { future.get(); } catch (Exception e) { if(log.isDebugEnabled()) log.debug("Task execution is interrupted. Context " + contextUuid); } finally { futures.remove(future); if(log.isDebugEnabled())log.debug("Task is detached from a context " + contextUuid); } if(futures.isEmpty()){ synchronized (tasks){ if(futures.isEmpty()){ tasks.remove(contextUuid); } } } } }); }  Future<T> runWithContext(UUID contextUuid, final Callable<T> callable); void cancelContext(UUID contextUuid);  }### Answer:
@Test public void attachToContext() throws ExecutionException, InterruptedException { final UUID contextUuid = UUID.randomUUID(); final Future future = mock(Future.class); final ArrayList<Future<?>> futures = new ArrayList<Future<?>>(); doReturn(futures).when(spy).getFuturesOfContext(contextUuid); final ArgumentCaptor<Runnable> runnableArgumentCaptor = ArgumentCaptor.forClass(Runnable.class); doReturn(null).when(executor).submit(runnableArgumentCaptor.capture()); spy.attachToContext(contextUuid, future); assertEquals(futures.size(), 1); assertSame(futures.get(0), future); final Runnable runnable = runnableArgumentCaptor.getValue(); assertNotNull(runnable); runnable.run(); assertTrue(futures.isEmpty()); verify(tasks).remove(contextUuid); }