### Question:
MovieRepositoryImplementation implements MovieRepository { @Override public Observable<List<Movie>> fetchMovies(final String category) { return Observable.defer(() -> movieDbRestService.fetchAllMovies(category) .concatMap(moviesList -> Observable.from(moviesList.getMovies()) .toList())) .retryWhen(observable -> observable.flatMap(o -> { if (o instanceof IOException) { return Observable.just(null); } return Observable.error(o); })); } MovieRepositoryImplementation(MovieDbRestService movieDbRestService); @Override Observable<List<Movie>> fetchMovies(final String category); @Override Observable<List<Trailer>> getMovieTrailers(long movieId); @Override Observable<List<Review>> getMovieReviews(long movieId);  }### Answer:
@Test public void fetchAllMovies_200OkResponse_InvokesCorrectApiCalls() { when(mMovieDbRestService.fetchAllMovies(anyString())) .thenReturn(Observable.just(moviesList())); TestSubscriber<List<Movie>> testSubscriber = new TestSubscriber<>(); mMovieRepository.fetchMovies(POPULAR).subscribe(testSubscriber); testSubscriber.awaitTerminalEvent(); testSubscriber.assertNoErrors(); List<List<Movie>> onNextEvents = testSubscriber.getOnNextEvents(); List<Movie> movies = onNextEvents.get(0); Assert.assertEquals(JURASIC_WORLD, movies.get(0).getOriginalTitle()); Assert.assertEquals(FAST_AND_FURIOUS, movies.get(1).getOriginalTitle()); verify(mMovieDbRestService).fetchAllMovies(POPULAR); }
@Test public void fetchAllMovies_IOExceptionThenSuccess_fetchMoviesRetried() { when(mMovieDbRestService.fetchAllMovies(anyString())) .thenReturn(getIOExceptionError(), Observable.just(moviesList())); TestSubscriber<List<Movie>> testSubscriber = new TestSubscriber<>(); mMovieRepository.fetchMovies(POPULAR).subscribe(testSubscriber); testSubscriber.awaitTerminalEvent(); testSubscriber.assertNoErrors(); verify(mMovieDbRestService, times(2)).fetchAllMovies(POPULAR); }
@Test public void fetchAllMovies_OtherHttpError_fetchMoviesTerminatedWithError() { when(mMovieDbRestService.fetchAllMovies(anyString())) .thenReturn(get403ForbiddenError()); TestSubscriber<List<Movie>> subscriber = new TestSubscriber<>(); mMovieRepository.fetchMovies(POPULAR).subscribe(subscriber); subscriber.awaitTerminalEvent(); subscriber.assertError(HttpException.class); verify(mMovieDbRestService).fetchAllMovies(POPULAR); }