### Question:
AsynchronousService { public <T> void execute(final AsyncTask<T> task) { try { task.onPreCall(); } catch (Exception e) { task.onError(e); return; } service.submit(new FutureTask<T>(task) { @Override protected void done() { super.done(); try { task.onPostCall(get()); } catch (InterruptedException e) { } catch (ExecutionException e) { task.onError(e.getCause()); } } }); } AsynchronousService(BlockingQueue<Runnable> workQueue); void execute(final AsyncTask<T> task); void close();  }### Answer:
@Test public void testPerfectExecution() throws Exception { final var result = new Object(); when(task.call()).thenReturn(result); service.execute(task); verify(task, timeout(2000)).onPostCall(eq(result)); final var inOrder = inOrder(task); inOrder.verify(task, times(1)).onPreCall(); inOrder.verify(task, times(1)).call(); inOrder.verify(task, times(1)).onPostCall(eq(result)); verifyNoMoreInteractions(task); }
@Test public void testCallException() throws Exception { final var exception = new IOException(); when(task.call()).thenThrow(exception); service.execute(task); verify(task, timeout(2000)).onError(eq(exception)); final var inOrder = inOrder(task); inOrder.verify(task, times(1)).onPreCall(); inOrder.verify(task, times(1)).call(); inOrder.verify(task, times(1)).onError(exception); verifyNoMoreInteractions(task); }
@Test public void testPreCallException() { final var exception = new IllegalStateException(); doThrow(exception).when(task).onPreCall(); service.execute(task); verify(task, timeout(2000)).onError(eq(exception)); final var inOrder = inOrder(task); inOrder.verify(task, times(1)).onPreCall(); inOrder.verify(task, times(1)).onError(exception); verifyNoMoreInteractions(task); }