Join Chiu-Ki Chan for an in-depth discussion in this video Test doubles, part of Effective Android Testing for Mobile Developers.
- [Instructor] What happens if we run this test again? Let's try that. Click on the green triangle to run click to favorite again. This time it failed. Let's take a look at the error message. It said, expected, not is selected, and then has this long list of attributes. If you scroll you can see that the view is selected is actually true. Scroll down in the errors and see where the failure actually happens.
It's in line 45. We are verifying that we started with the recipe as not selected. And the reason why it failed is because, when we ran this test previously we clicked on the recipe and stored the variables selected into our shared preferences. This is why when we run the test a second time it will not pass. When the test changes the state of the app, we cannot run it again and expect the same result.
How to solve this problem? We want it so that in the app it will always remember our selection, but in our test, we want it to start as a known state. To solve this problem we can use dependency injection with test doubles. A test double is like a stunt double. It stands in place of the actual object during test. In our case, instead of storing favorites in shared preferences, we will store them in an in-memory hash mark.
That way, the hash mark is always empty when we start the test, regardless of what we did to it previously. However, we still want to use shared preferences during normal app operation so that it remembers the favorites when we come back to the app later. How do we only swap out shared preferences' favorites with in-memory favorites during test? We are going to use dependency injection. We'll take a look at that next.
- 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.