### Question:
Task implements Runnable { @Override public void run() { try { T object = execute(); if (listener != null) { listener.onTaskComplete(object); } } catch (Exception e) { if (listener != null) { listener.onTaskError(e); } } } Task(@Nullable Listener<T> listener); @Override void run();  }### Answer:
@Test public void run_verifyListenerCallback() { Task.Listener<String> listener = mock(Task.Listener.class); Task<String> task = new Task<String>(listener) { @Override String execute() { return "test"; } }; task.run(); verify(listener).onTaskComplete("test"); verifyNoMoreInteractions(listener); }
@Test public void run_throwException_verifyListenerCallback() { Task.Listener<String> listener = mock(Task.Listener.class); Task<String> task = new Task<String>(listener) { @Nullable @Override String execute() throws Exception { throw new RuntimeException(); } }; task.run(); verify(listener).onTaskError(any(RuntimeException.class)); verifyNoMoreInteractions(listener); }