### Question:
UserRetriever { public void retrieveUser(final UserCallback callback) { mExecutor.execute(new RetrieveUserTask(callback)); } UserRetriever(final WebService webService, final AsyncTaskExecutor executor); void retrieveUser(final UserCallback callback);  }### Answer:
@Test public void retrieveUser_retrievesUser_andNotifiesCallback() throws IOException { User user = new User(); WebService webService = mock(WebService.class); when(webService.retrieveUser()).thenReturn(user); UserRetriever.UserCallback callback = mock(UserRetriever.UserCallback.class); UserRetriever userRetriever = new UserRetriever(webService, TestAsyncTaskExecutor.instance()); userRetriever.retrieveUser(callback); verify(callback).onUserRetrieved(user); }
@Test public void retrieveUser_retrievesUser_withException_notifiesCallback() throws IOException { IOException e = new IOException(); WebService webService = mock(WebService.class); when(webService.retrieveUser()).thenThrow(e); UserRetriever.UserCallback callback = mock(UserRetriever.UserCallback.class); UserRetriever userRetriever = new UserRetriever(webService, TestAsyncTaskExecutor.instance()); userRetriever.retrieveUser(callback); verify(callback).onException(e); }