DefaultAccountManager implements AccountManager { @Override public Observable<Account> doLogin(AuthCredentials credentials) { return Observable.just(credentials).flatMap(new Func1<AuthCredentials, Observable<Account>>() { @Override public Observable<Account> call(AuthCredentials credentials) { try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } if (credentials.getUsername().equals("ted") && credentials.getPassword().equals("robin")) { currentAccount = new Account(); return Observable.just(currentAccount); } return Observable.error(new LoginException()); } }); }  @Override Observable<Account> doLogin(AuthCredentials credentials); @Override Account getCurrentAccount(); @Override boolean isUserAuthenticated();  }
@Test public void loginSuccessful() { AccountManager manager = new DefaultAccountManager(); manager.doLogin(new AuthCredentials("ted", "robin")).subscribe(new Subscriber<Account>() { @Override public void onCompleted() { } @Override public void onError(Throwable e) { Assert.fail("Login failed, but should be successful"); } @Override public void onNext(Account account) { Assert.assertEquals("Ted Mosby", account.getName()); } }); }
@Test public void loginFail() { AccountManager manager = new DefaultAccountManager(); manager.doLogin(new AuthCredentials("Foo", "Password")).subscribe(new Subscriber<Account>() { @Override public void onCompleted() { Assert.fail("Login successful, but should be fail"); } @Override public void onError(Throwable e) { Assert.assertTrue(e instanceof LoginException); } @Override public void onNext(Account account) { } }); }