Join Chiu-Ki Chan for an in-depth discussion in this video Test state: Setup, part of Effective Android Testing for Mobile Developers.
- Now that we have a separate in memory version of Favorites, we can set it up to be in a certain state before launching our activity and verify that the UI is what we expect. To do that, we'll need to have access to Favorites during Test. In RecipeActivityTest, scroll up. Before the TestRule, we're going to add a Member Variable. In line 26, right after the activityRule, add a new variable.
Private InMemoryFavorites and we'll call it favorites. To initialize that, we will add a Before Method. @Before and then public void clearFavorites. This means that we are going to run this before each test method. This way we know that we will always have a clean InMemoryFavorites. The first thing we need to do is to get the application.
TestRecipeApplication app = we'll cast it to TestRecipeApplication and then call Instrumentation Registry dot getTargetContext dot getApplicationContext. Now that we have a app we can use it to initialize favorites. Favorites = app.getFavorites. Alt + Enter to cast it to the correct type.
Finally we are calling this method clearFavorites so we should add a method called Clear. Favorites.clear. Alt + Enter to create a method. We would like to create it just in InMemoryFavorites because we are only going to clear it when we are testing. Void as the return type is great. Inside the clear function we will call map.clear. Now we have a clean InMemoryFavorites per test method.
Next we will write the test to verify the UI when the recipe has already been marked as a Favorite.
- 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.