MovieDetailsPresenter implements MovieDetailsMVP.Presenter { @Override public void startPresenting(final MovieDetailsMVP.View movieDetailsView, long movieId) { MovieDetails movieDetails = movieDetailsUseCase.getMovieDetails(); if (movieDetails != null) { movieDetailsView.display(movieDetails); } else { movieDetailsUseCase.setListener(new MovieDetailsUseCase.Listener() { @Override public void onMovieDetails(MovieDetails movieDetails) { movieDetailsView.display(movieDetails); } @Override public void onError(Throwable throwable) { movieDetailsView.showError(); } }); movieDetailsUseCase.loadMovieDetails(movieId); } } MovieDetailsPresenter(MovieDetailsUseCase movieDetailsUseCase); @Override void startPresenting(final MovieDetailsMVP.View movieDetailsView, long movieId); @Override void stopPresenting();  }
@Test public void shouldDisplayMovieDetailsIfAvailable() throws Exception { when(movieDetailsUseCase.getMovieDetails()).thenReturn(ANY_MOVIE_DETAILS); presenter.startPresenting(view, ANY_MOVIE_ID); verify(view).display(ANY_MOVIE_DETAILS); verify(movieDetailsUseCase, never()).loadMovieDetails(anyLong()); verify(movieDetailsUseCase, never()).setListener(any(MovieDetailsUseCase.Listener.class)); }
@Test public void shouldSetListenerAndLoadMovieDetailsIfNotAvailable() throws Exception { when(movieDetailsUseCase.getMovieDetails()).thenReturn(null); presenter.startPresenting(view, ANY_MOVIE_ID); InOrder order = inOrder(movieDetailsUseCase); order.verify(movieDetailsUseCase).setListener(any(MovieDetailsUseCase.Listener.class)); order.verify(movieDetailsUseCase).loadMovieDetails(ANY_MOVIE_ID); }
@Test public void shouldBindListenerToView() throws Exception { Exception exception = new Exception(); when(movieDetailsUseCase.getMovieDetails()).thenReturn(null); ArgumentCaptor<MovieDetailsUseCase.Listener> listenerCapture = ArgumentCaptor.forClass(MovieDetailsUseCase.Listener.class); presenter.startPresenting(view, ANY_MOVIE_ID); verify(movieDetailsUseCase).setListener(listenerCapture.capture()); MovieDetailsUseCase.Listener listener = listenerCapture.getValue(); listener.onMovieDetails(ANY_MOVIE_DETAILS); verify(view).display(ANY_MOVIE_DETAILS); listener.onError(exception); verify(view).showError(); }