PokedexPager { public Observable<List<PokemonItem>> pokemon() { return Observable.concat(nextRequests.map(new Func1<HttpUrl, Observable<Page<PokemonItem>>>() { @Override public Observable<Page<PokemonItem>> call(HttpUrl url) { Single<Page<PokemonItem>> call = url == null ? api.firstPokemonPage() : api.nextPokemonPage(url.toString()); return call.doOnSuccess(new Action1<Page<PokemonItem>>() { @Override public void call(Page<PokemonItem> page) { nextPage.set(page.next()); } }).toObservable(); } })).takeUntil(new Func1<Page<PokemonItem>, Boolean>() { @Override public Boolean call(Page<PokemonItem> page) { return page.next() == null; } }).map(new Func1<Page<PokemonItem>, List<PokemonItem>>() { @Override public List<PokemonItem> call(Page<PokemonItem> page) { return page.results(); } }); } @Inject  PokedexPager(PokeService api); Observable<List<PokemonItem>> pokemon(); void setNextPage(@Nullable HttpUrl nextPage); @Nullable HttpUrl nextPage(); void requestNextPage();  }
@Test public void immediatelyReturnsFirstPage() throws Exception { PokeService service = mock(PokeService.class); when(service.firstPokemonPage()).thenReturn(Single.just(Page.create(0, null, null, Arrays.asList( PokemonItem.create("bulbasaur", HttpUrl.parse("http: )))); PokedexPager pager = new PokedexPager(service); TestSubscriber<List<PokemonItem>> subscriber = TestSubscriber.create(); pager.pokemon().subscribe(subscriber); subscriber.assertReceivedOnNext(Arrays.asList( Arrays.asList(PokemonItem.create("bulbasaur", HttpUrl.parse("http: )); subscriber.assertCompleted(); }