title: How to test presenter on android (MVP)?
body: <p>I've never create unit testing before. I'm planning to create UI test &amp; Unit test for my presenter &amp; datasource. I use Retrofit, RxJava, and Dagger in my apps. </p>
  
  <p>Here's what i've tried so far</p>
  
  <p>DataSource (My Datasource is coming from API)</p>
  
  <pre><code>public class DataSource implements DataSourceContract {
  
      private static DataSource dataSource;
  
      @Inject
      SharedPreferences sharedPreferences;
      @Inject
      NewsService newsService;
  
      private DataSource(Context context) {
          DaggerAppComponent.builder()
                  .networkModule(new NetworkModule(API_URL))
                  .appModule(new AppModule(context.getApplicationContext()))
                  .preferencesModule(new PreferencesModule())
                  .build()
                  .inject(this);
      }
  
      public static synchronized DataSource getInstance(Context context) {
          if(dataSource == null) {
              dataSource = new DataSource(context);
          }
  
          return dataSource;
      }
  
      public String parseError(Throwable e) {
          if(e instanceof SocketTimeoutException) {
              return ERROR_TIMEOUT;
          }
          else if(e instanceof SocketException) {
              return ERROR_NO_CONNECTION;
          }
          else {
              return ERROR_SERVER;
          }
      }
  
      @Override
      public DisposableObserver&lt;NewsResponse&gt; getNews(final Callback&lt;NewsResponse&gt; callback) {
          return newsService.getNews()
                  .subscribeOn(Schedulers.io())
                  .observeOn(AndroidSchedulers.mainThread())
                  .subscribeWith(new DisposableObserver&lt;NewsResponse&gt;() {
                      @Override
                      public void onNext(NewsResponse value) {
                          callback.onSuccess(value);
                      }
  
                      @Override
                      public void onError(Throwable e) {
                          callback.onFailure(e);
                      }
  
                      @Override
                      public void onComplete() {
  
                      }
                  });
      }
  }
  </code></pre>
  
  <p>Presenter</p>
  
  <pre><code>public class MainPresenter implements MainContract.Presenter {
      private MainContract.View view;
      private DataSource dataSource;
      private Disposable dispossable;
  
      public MainPresenter(MainContract.View view, DataSource dataSource) {
          this.view = view;
          this.dataSource = dataSource;
      }
  
      @Override
      public void onStart() {
          getNews();
      }
  
      @Override
      public void onStop() {
          if(dispossable != null &amp;&amp; !dispossable.isDisposed()) {
              dispossable.dispose();
          }
      }
  
      @Override
      public void getNews() {
          view.setLoading(true);
  
          dispossable = dataSource.getNews(new DataSourceContract.Callback&lt;NewsResponse&gt;() {
              @Override
              public void onSuccess(NewsResponse responseData) {
                  try {
                      switch (responseData.getStatus()) {
                          case API_SUCCESS:
                              view.setLoading(false);
                              view.getNewsSuccess(responseData.getArticles());
                              break;
                          default:
                              view.setLoading(false);
                              view.getNewsFailed(responseData.getStatus());
                              break;
  
                      }
                  }
                  catch (Exception e) {
                      view.setLoading(false);
                      view.getNewsFailed(ERROR_SERVER);
                  }
              }
  
              @Override
              public void onFailure(Throwable e) {
                  view.setLoading(false);
                  view.isNetworkFailed(dataSource.parseError(e), false);
              }
          });
      }
  }
  </code></pre>
  
  <p>And this is the test of my presenter</p>
  
  <pre><code>public class MainPresenterTest {
      @Mock
      DataSource dataSource;
  
      @Mock
      MainContract.View view;
  
      MainContract.Presenter presenter;
  
      @Before
      public void setup() {
          MockitoAnnotations.initMocks(this);
          presenter = new MainPresenter(view, dataSource);
      }
  
      @Test
      public void getNews() throws Exception {
          List&lt;Article&gt; articleList = new ArrayList&lt;&gt;();
          presenter.getNews();
          Mockito.verify(view, Mockito.only()).getNewsSuccess(articleList);
      }
  }
  </code></pre>
  
  <p>But there is error when I run the test</p>
  
  <pre><code>Wanted but not invoked:
  view.getNewsSuccess([]);
  -&gt; at com.java.mvp.view.main.MainPresenterTest.getNews(MainPresenterTest.java:37)
  </code></pre>
  
  <p>I have no problem running this apps on the device, but I can't make it work on testing</p>
  
  <p>Any idea how to fix this presenter test? Am I doing it right?</p>
  
  <p>And how do I test my datasource? I have no idea how to test this one</p>
  
  <p>Thank you</p>