DataManager { public Single<AccountData> getAccountDetails() { return getAccounts() .flatMap(new Func1<AccountsResponse, Single<? extends AccountData>>() { @Override public Single<? extends AccountData> call(AccountsResponse accountsResponse) { if (!accountsResponse.accounts.isEmpty()) { String accountId = accountsResponse.accounts.get(0).id; return Single.zip(getBalance(accountId), getTransactions(accountId), new Func2<Balance, List<Transaction>, AccountData>() { @Override public AccountData call(Balance balance, List<Transaction> transactions) { return new AccountData(balance, transactions); } }); } return Single.just(new AccountData()); } }); } @Inject  DataManager(MondroidService mondroidService, PreferencesHelper preferencesHelper); Single<AccessToken> getAccessToken(String code); Single<Boolean> isUserSignedIn(); Completable signOut(); Single<AccountsResponse> getAccounts(); Single<AccountData> getAccountDetails(); Single<Balance> getBalance(String accountId); Single<List<Transaction>> getTransactions(String accountId);  }
@Test public void getAccountDetailsCompletesAndEmitsAccountDetails() { stubPreferencesHelperGetAccessToken(TestDataFactory.makeAccessToken()); List<Account> accounts = TestDataFactory.makeAccounts(1); MondroidService.AccountsResponse accountsResponse = new MondroidService.AccountsResponse(); accountsResponse.accounts = accounts; stubMondoServiceGetAccounts(Single.just(accountsResponse)); Balance balance = TestDataFactory.makeBalance(); stubMondoServiceGetBalance(Single.just(balance)); List<Transaction> transactions = TestDataFactory.makeTransactions(5); MondroidService.TransactionResponse transactionResponse = new MondroidService.TransactionResponse(); transactionResponse.transactions = transactions; stubMondoServiceGetTransactions(Single.just(transactionResponse)); DataManager.AccountData accountData = new DataManager.AccountData(balance, transactions); TestSubscriber<DataManager.AccountData> testSubscriber = new TestSubscriber<>(); mDataManager.getAccountDetails().subscribe(testSubscriber); testSubscriber.assertCompleted(); testSubscriber.assertValue(accountData); }