MainActivityPresenter extends BasePresenter { @Override public void onUpdateComplete(RequestResult requestResult, ViewModel.QueryEnum query) { if (query instanceof MainActivityViewModel.QueryEnumMainActivity) { if (requestResult == null) { return; } MainActivityViewModel.WeatherRequestResult weatherRequestResult = (MainActivityViewModel.WeatherRequestResult) requestResult; if (MainActivityViewModel.QueryEnumMainActivity.UPDATE_WEATHER == query) { RequestStatus requestStatus = weatherRequestResult.getRequestStatus(); if (requestStatus == RequestStatus.SUCESS) { mDisplayView.hideProgressBar(); handleWeatherUpdate(weatherRequestResult); } else if (requestStatus == RequestStatus.FAILED) { Throwable throwable = weatherRequestResult.getThrowable(); if (throwable != null) { String errorMessage = throwable.getMessage(); mDisplayView.showDialog(errorMessage); } mDisplayView.hideProgressBar(); } else { handleWeatherUpdate(weatherRequestResult); } } } else { throw new IllegalArgumentException("request result not handled here"); } } MainActivityPresenter(int presenterId, DisplayView displayView, ViewModel viewModel); @Override void loadInitialData(Bundle args, boolean isConfigurationChange); @Override void onUpdateComplete(RequestResult requestResult, ViewModel.QueryEnum query); void onSearchButtonClicked(String city);  }
@Test public void testOnUpdateComplete() throws Exception { when(mWeatherRequestResult.getRequestStatus()).thenReturn(RequestStatus.SUCESS); when(mWeatherRequestResult.getCity()).thenReturn(city); mPresenter.onUpdateComplete(mWeatherRequestResult, MainActivityViewModel.QueryEnumMainActivity.UPDATE_WEATHER); verify(mHomepageCallbacks).showCityName(city); }
@Test public void testFailCase(){ when(mWeatherRequestResult.getRequestStatus()).thenReturn(RequestStatus.FAILED); when(mWeatherRequestResult.getThrowable()).thenReturn(new Exception("This is a mocked exception!")); mPresenter.onUpdateComplete(mWeatherRequestResult, MainActivityViewModel.QueryEnumMainActivity.UPDATE_WEATHER); verify(mHomepageCallbacks).hideProgressBar(); verify(mHomepageCallbacks).showDialog(anyString()); }