HouseRepository implements HousesDomain.Repository { @Override public Observable<List<GoTHouse>> getList() { Observable<List<GoTHouse>> observable; if (localDataSource.isExpired()) { observable = networkDataSource.getAll() .doOnNext(houses -> { localDataSource.removeAll(houses); localDataSource.save(houses); }) .retry((attempts, error) -> error instanceof Exception && attempts < 2) .onErrorResumeNext(throwable -> { return localDataSource.getAll(); }); } else { observable = localDataSource.getAll(); } return observable; } HouseRepository(HousesDomain.NetworkDataSource networkDataSource,
                         HousesDomain.LocalDataSource localDataSource); @Override Observable<List<GoTHouse>> getList();  }
@Test public void should_not_return_any_house() throws Exception { when(localDataSource.isExpired()).thenReturn(EXPIRED); when(networkDataSource.getAll()).thenReturn(getEmptyHouseListObservable()); repository.getList() .subscribe(list -> Assert.assertThat(list.size(), Is.is(0)), throwable -> fail()); verify(networkDataSource).getAll(); verify(localDataSource).save(anyList()); verify(localDataSource, never()).getAll(); }
@Test public void should_return_all_houses() throws Exception { when(localDataSource.isExpired()).thenReturn(EXPIRED); when(networkDataSource.getAll()).thenReturn(getSevenHousesObservable()); repository.getList() .subscribe(list -> Assert.assertThat(list.size(), Is.is(7)), throwable -> fail()); verify(networkDataSource).getAll(); verify(localDataSource).save(anyList()); verify(localDataSource, never()).getAll(); }