in an Activity or a Fragment. Any code that does not handle a UI or operating system interaction should not be in these classes. Keeping them as lean as possible will allow you to avoid many lifecycle related problems. --Android Developers Guide https://developer.android.com/jetpack/docs/guide Separation of Concerns
No Robolectric or Espresso • Foundation for working UI logic Why is unit testing hard in Android? • Android life cycle • Lots of dependencies Unit Testing
• TripListPresenter • TripListContract ◦ IActivity ◦ IModel ◦ IView ◦ IPresenter cmd+alt+b Goto Implementation Works on methods to find where they are implemented/overriden.
model.getTripCount(); //Set up the test so that tripDeleted is true boolean tripDeleted = true; //Call the method of interest presenter.handleActivityResult(requestCode, resultCode, index, tripDeleted); //Assert that list is 1 trip fewer int tripCountAfter = model.getTripCount(); assertEquals(tripCountAfter, tripCountBefore - 1); } Write Tests
model.getTripCount(); //Set up the test so that tripDeleted is true boolean tripDeleted = true; //Call the method of interest presenter.handleActivityResult(requestCode, resultCode, index, tripDeleted); //Assert that list is 1 trip fewer int tripCountAfter = model.getTripCount(); assertEquals(tripCountAfter, tripCountBefore - 1); } Write Tests
model.getTripCount(); //Set up the test so that tripDeleted is true boolean tripDeleted = true; //Call the method of interest presenter.handleActivityResult(requestCode, resultCode, index, tripDeleted); //Assert that list is 1 trip fewer int tripCountAfter = model.getTripCount(); assertEquals(tripCountAfter, tripCountBefore - 1); } Write Tests
model.getTripCount(); //Set up the test so that tripDeleted is true boolean tripDeleted = true; //Call the method of interest presenter.onActivityResult(requestCode, resultCode, mockIntent); //Assert that list is 1 trip fewer int tripCountAfter = model.getTripCount(); assertEquals(tripCountAfter, tripCountBefore - 1); } Write Tests
test doNothing().when(mockWebClient.beginSync()) //Call the method of interest presenter.triggerActivityPushSync(); //Assert that the progress bar is showing assertTrue(mockView.inProgress()); } Write Tests
test doNothing().when(mockWebClient.beginSync()) //Call the method of interest presenter.triggerActivityPushSync(); //Assert that the progress bar is showing assertTrue(mockView.inProgress()); } Write Tests
test doNothing().when(mockWebClient.beginSync()) //Call the method of interest presenter.triggerActivityPushSync(); //Assert that the progress bar is showing assertTrue(mockView.inProgress()); } Write Tests
when the activity gets destroyed Pros and Cons Pros • Can now achieve separation of concerns • Can now test previously huge and untestable activities/fragments