### Question:
ContextExecutorService { public <T> Future<T> runWithContext(UUID contextUuid, final Callable<T> callable) { final SecurityContext context = SecurityContextHolder.getContext(); final Future<T> future = executor.submit(new Callable<T>() { @Override public T call() throws Exception { SecurityContextHolder.setContext(context); return callable.call(); } }); attachToContext(contextUuid, future); return future; }  Future<T> runWithContext(UUID contextUuid, final Callable<T> callable); void cancelContext(UUID contextUuid);  }### Answer:
@Test public void runWithContext() throws Exception { final Callable callable = mock(Callable.class); final Object expectedCallableResult = new Object(); doReturn(expectedCallableResult).when(callable).call(); final UUID contextUuid = UUID.randomUUID(); final Future expectedFuture = mock(Future.class); final ArgumentCaptor<Callable> callableArgumentCaptor = ArgumentCaptor.forClass(Callable.class); when(executor.submit(callableArgumentCaptor.capture())).thenReturn(expectedFuture); final SecurityContext initialContext = SecurityContextHolder.getContext(); final SecurityContext securityContextMock = mock(SecurityContext.class); SecurityContextHolder.setContext(securityContextMock); final Future result = spy.runWithContext(contextUuid, callable); assertSame(result, expectedFuture); verify(spy).attachToContext(contextUuid, expectedFuture); final Callable callableWrapper = callableArgumentCaptor.getValue(); SecurityContextHolder.clearContext(); assertNotSame(SecurityContextHolder.getContext(), securityContextMock); final Object actualCallableResult = callableWrapper.call(); assertSame(SecurityContextHolder.getContext(), securityContextMock); assertSame(actualCallableResult, expectedCallableResult); SecurityContextHolder.setContext(initialContext); }