### Question:
ReportDownloader { public Thread whenReportReady(final ReportCallback callback) { Preconditions.checkNotNull(callback, "Report callback cannot be null."); Thread waitThread = new Thread("ReportUtils.whenReportReady " + reportJobId) { @Override public void run() { try { if (waitForReportReady()) { callback.onSuccess(); } else { callback.onFailure(); } } catch (RemoteException e) { callback.onException(e); } catch (InterruptedException e) { callback.onInterruption(); } catch (RuntimeException e) { callback.onException(e); } } }; waitThread.start(); return waitThread; } ReportDownloader(ReportServiceInterface reportService, long reportJobId); @VisibleForTesting  ReportDownloader(ReportServiceInterface reportService, long reportJobId, Sleeper sleeper); Thread whenReportReady(final ReportCallback callback); boolean waitForReportReady(); URL getDownloadUrl(ReportDownloadOptions options); CharSource getReportAsCharSource(ReportDownloadOptions options); static final Charset REPORT_CHARSET; static final int SLEEP_TIMER; }### Answer:
@Test public void testWhenReportReady_complete() throws IOException, InterruptedException { ReportDownloader downloader = new ReportDownloader(reportService, 1); when(reportService.getReportJobStatus(Matchers.anyLong())) .thenReturn(ReportJobStatus.COMPLETED); ReportCallback callback = mock(ReportCallback.class); downloader.whenReportReady(callback).join(); Mockito.verify(callback).onSuccess(); }
@Test public void testWhenReportReady_failed() throws IOException, InterruptedException { ReportDownloader downloader = new ReportDownloader(reportService, 1); when(reportService.getReportJobStatus(Matchers.anyLong())).thenReturn(ReportJobStatus.FAILED); ReportCallback callback = mock(ReportCallback.class); downloader.whenReportReady(callback).join(); Mockito.verify(callback).onFailure(); }
@Test public void testWhenReportReady_remote() throws IOException, InterruptedException { ReportDownloader downloader = new ReportDownloader(reportService, 1); RemoteException e = new RemoteException(); when(reportService.getReportJobStatus(Matchers.anyLong())).thenThrow(e); ReportCallback callback = mock(ReportCallback.class); downloader.whenReportReady(callback).join(); Mockito.verify(callback).onException(e); }