Join Chiu-Ki Chan for an in-depth discussion in this video Testing with MVP: Exception, part of Effective Android Testing for Mobile Developers.
- [Narrator] Now we will test the togFavorites function without loading a recipe first. If you go to the Presenter, scroll down, and look at the togFavorite function. Remember when we were writing it, I was like, "Oh! If the recipe is null, it's going to be bad." So let's try that. Let's actually try the case when we have a presenter but we have no code, no recipe yet. So recipe is null, and then in toggleFavorite we will try to read this ID.
Go back to the test. Once again, when you're on the class itself, when you press Command+Shift+T, you can switch to the associated test. We'll add a new test method, @Test to indicate that it is a test method, and then public void, we will call it togglewithoutLoad. Since we have initialized the presenter in the setup method, all we need to do here is to code toggle.
So presenter.toggleFavorite. How are we going to verify that it actually throws an exception? Well one thing you can do is to just run the test. So let's do that. Click on the green triangle and press run. So here in the test it threw an illegal state exception, which is what we want it to do if we try to toggle a favorite without first loading a recipe. However, this is the failing test case.
We don't actually want to have a test case where we expect it to fail. Fortunately, in the JUnit framework, we can specify and expect an exception. All you need to do is to go to the test annotation on line 34 and then @expected equal, in our case, IllegalStateException, dot class. Now, when we run the test, it passes.
This means that JUnit ran the test method and verified that it is throwing IllegalStateException, and therefore calling it a passing test. Next, we will test the case when we are actually loading a recipe.
- 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 Davis4h 52m 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.