Join Chiu-Ki Chan for an in-depth discussion in this video Refactor: Load recipe, part of Effective Android Testing for Mobile Developers.
- [Instructor] We have identified the five things that RecipeActivity do. Let's refactor them into the presenter one by one. Step one only sets up the UI. There is no logic so it stays in the activity. Step two extracts the recipe ID from the intent in line 30. This part stays in the activity because there's no other way to get hold of the intent. In line 31 we use this ID to load the recipe.
This can go inside the presenter as long as the presenter is initialized with a RecipeStore. So, let's do that. In the UI Recipe folder that we created the contract, right click, New, Java Class. We will call it RecipePresenter. Add a private final field RecipeStore, private final RecipeStore and we'll name it store. Alt enter to add a constructor parameter.
Now that we have a store, we can use it to load the recipe when someone give us an ID. Add a new function. Public void loadRecipe. Pass it the ID as a string. Recipe equal store.getRecipe ID. Great, now we have a recipe. What do we do with it? We have two choices, either return it back to the activity or store it as a field within the presenter.
Our goal here is to extract as much logic as possible out of the activity so a good rule of thumb is that if it does not involve Android, it stays inside the presenter. Since recipe is a pure Java object, it can stay in the presenter as a field. Put your cursor at recipe and press alt enter to create the field. Let's switch back to the activity and use the presenter. In line 31 instead of loading the recipe directly from the store, we will do it through the presenter.
First, we will need the presenter, so let's add it before line 31. RecipePresenter presenter equal new RecipePresenter. The first parameter is store and we have one from line 29. Next, we will call loadRecipe on the presenter. Presenter.loadRecipe and then the ID which is defined in line 30. With that we don't need line 33 anymore.
Great, that take care of step two. Next, let's do step three to show an error when the recipe is null.
- Why test?
- Local vs. on-device
- Code coverage
- UI testing
- Hermetic environment
- Dependency injection
- Testing with MVP
Skill Level Intermediate
Android App Development: Unit Testingwith James P White2h 58m Intermediate
Android Studio Essential Trainingwith David Gassner3h 41m Beginner
Android App Development: Data Persistence Librarieswith Annyce Davis3h 59m Intermediate
1. Testing Considerations
2. Test as You Go
3. UI Testing
4. Hermetic Environment
5. Robot Pattern
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.Cancel
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.