MailProvider { public Observable<Mail> getMail(final int id) { return getFilteredMailList(new Func1<Mail, Boolean>() { @Override public Boolean call(Mail mail) { return mail.getId() == id; } }).flatMap(new Func1<List<Mail>, Observable<Mail>>() { @Override public Observable<Mail> call(List<Mail> mails) { if (mails == null || mails.isEmpty()) { return Observable.error(new NotFoundException()); } return Observable.just(mails.get(0)); } }); } MailProvider(AccountManager accountManager, MailGenerator genrator); Observable<List<Label>> getLabels(); Observable<Mail> getMail(final int id); Observable<List<Mail>> getMailsOfLabel(final String l); Observable<Mail> markAsRead(final Mail mail, final boolean read); Observable<Mail> starMail(int mailId, final boolean star); Observable<Mail> addMailWithDelay(final Mail mail); Observable<Mail> addMail(final Mail mail); Observable<Mail> setLabel(Mail mail, final String label); Observable<Mail> setLabel(int mailId, final String label); Observable<List<Mail>> searchForMails(String query, final int limit); Observable<List<Mail>> searchForOlderMails(final String query, final Date olderAs,
      final int limit); Observable<List<Mail>> getMailsSentBy(final int personId); Observable<MailStatistics> getStatistics(); static int DELAY; static int authExceptionEach; static int errorEach; static final Label INBOX_LABEL; }
@Test public void getMailById() { final AtomicBoolean mailFound = new AtomicBoolean(false); final int id = 1; mailProvider.getMail(id).subscribe(new Subscriber<Mail>() { @Override public void onCompleted() { } @Override public void onError(Throwable e) { Assert.fail("Could not find mail with the id = " + id); } @Override public void onNext(Mail mail) { Assert.assertEquals(id, mail.getId()); mailFound.set(true); } }); Assert.assertTrue(mailFound.get()); final AtomicBoolean errorThrown = new AtomicBoolean(false); mailProvider.getMail(-1).subscribe(new Subscriber<Mail>() { @Override public void onCompleted() { } @Override public void onError(Throwable e) { Assert.assertTrue(e instanceof NotFoundException); errorThrown.set(true); } @Override public void onNext(Mail mail) { Assert.fail("A mail with id = " + id + " found, but a mail with this id should not exist"); } }); Assert.assertTrue(errorThrown.get()); }