title: Unit testing RxJava in MVP presenter in android
body: <p>I am new to TDD. Also new to MVP and Rxjava. I just dive into it and It is worth it. But I stuck at the testing part. I understand the basis of unit testing. It is a little bit difficult for me in beginning. But I stuck here and So how can test the presenter?</p>
  
  <p>Here is the Presenter class - </p>
  
  <pre><code>public class NewsPresenter {
  
  private final RxjavaService service;
  private final MainView view;
  private CompositeSubscription subscriptions;
  
  public NewsPresenter(RxjavaService service, MainView view) {
      this.service = service;
      this.view = view;
      subscriptions = new CompositeSubscription();
  }
  
  public void getNewsList(String urlQ){
      view.showWait();
  
      Subscription subscription = service.getNews(urlQ ,new RxjavaService.GetNewsCallback() {
          @Override
          public void onSuccess(Articles articles) {
              view.removeWait();
              view.getNewsListSuccess(articles);
          }
  
          @Override
          public void onError(NetworkError networkError) {
              view.removeWait();
              view.onFailure(networkError.getAppErrorMessage());
              Log.i("huh",networkError.getMessage());
          }
      });
  
      subscriptions.add(subscription);
  }
  
  public void onStop(){
      subscriptions.unsubscribe();
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Here is the View Interface -</p>
  
  <pre><code>public interface MainView {
  
  void showWait();
  
  void removeWait();
  
  void onFailure(String appErrorMessage);
  
  void getNewsListSuccess(Articles articles);
  </code></pre>
  
  <p>}</p>
  
  <p>Here is the RxJavaService class -</p>
  
  <pre><code>public class RxjavaService {
  
  private final NewsRestService newsRestService;
  
  public RxjavaService(NewsRestService newsRestService) {
      this.newsRestService = newsRestService;
  }
  
  
  public interface GetNewsCallback {
      void onSuccess(Articles articles);
  
      void onError(NetworkError networkError);
  }
  
  
  public Subscription getNews(String q, final GetNewsCallback getNewsCallback) {
      Log.i("stuck","service called");
      return newsRestService.getNewsBySearch(q,"8dca7dea475e41e49518b2c61131e118",100)
              .subscribeOn(Schedulers.io())
              .observeOn(AndroidSchedulers.mainThread())
              .onErrorResumeNext(new Func1&lt;Throwable, Observable&lt;? extends Articles&gt;&gt;() {
                  @Override
                  public Observable&lt;? extends Articles&gt; call(Throwable throwable) {
                      return Observable.error(throwable);
                  }
              })
              .subscribe(new Subscriber&lt;Articles&gt;() {
                  @Override
                  public void onCompleted() {
                      Log.i("stuck","complete");
                  }
  
                  @Override
                  public void onError(Throwable e) {
                      getNewsCallback.onError(new NetworkError(e));
                      Log.i("stuck",e.getMessage());
                  }
  
                  @Override
                  public void onNext(Articles articles) {
                      getNewsCallback.onSuccess(articles);
                      Log.i("stuck","Onnext");
                  }
              });
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Here is the Test class where I am stuck-</p>
  
  <pre><code>@RunWith(MockitoJUnitRunner.class)
  
  public class NewsListTest {
  
  private NewsPresenter newsPresenter;
  
  @Mock
  private RxjavaService rxjavaService;
  @Mock
  private MainView mainView;
  
  @Before
  public void setUp() throws Exception {
      MockitoAnnotations.initMocks(this);
  
      newsPresenter = new NewsPresenter(rxjavaService,mainView);
  }
  
  @After
  public void tearDown() throws Exception {
      mainView = null;
      newsPresenter.onStop();
  }
  
  @Test
  public void Testing_The_Result() {
  
  }
  </code></pre>
  
  <p>}</p>