Join Chiu-Ki Chan for an in-depth discussion in this video Test state: Test, part of Effective Android Testing for Mobile Developers.
- [Voiceover] Let's add a new test method for the case where recipe is already a favorite. Go back to recipe activity test, you can do that by pressing command o, and search for recipe activity test. Scroll down. You will need to launch the activity, just like in click to favorite. So let's extract that into a helper method. Highlight line fifty three through fifty five. Right click, refactor, extract, method.
We will call it launch recipe. Press okay. In launch recipe, we will add a parameter called ID. String ID, and then we will use the ID instead of creamed carrots in the put extra in line sixty four. Now, in line fifty three, it is unhappy, because now launch recipe takes a parameter. We are going to go ahead and add it, but this time we'll define it as a constant.
The reason why we want it to be a constant is because we will reuse that in another test method. We will call this constant carrots underscore ID. Press alt enter to create a constant field. Make sure that it actually matches the ID. So to be completely sure, what you can do is press command shift alt to open up the carrots recipe. Then, copy the other ID, and go back to recipe activity test to paste it in.
After we did this refactoring, let's run the test to make sure that everything still works. Scroll to the top of the file, and press the green triangles. Run. This will run both of our test methods instead of just click to favorite. And all the tests passed. Now we are ready to add another test method. Scroll back down. After click to favorite in line sixty two, add at test, and then public void already favorite.
What we are going to do here is that we are going to set it to be favorite before we launch the recipe. So favorite dot put, and then we are going to need an ID, which is carrots ID, comma, true, meaning that it is our favorite. Then, we're going to launch recipe. We'll launch recipe with the same ID that we already set favorites to. This is why, again, we are having a constant. If the ID doesn't match, you will be setting something, but reading something later in the activity.
Finally, you want to verify that the title is selected, meaning that it is a favorite. Unview, with ID, add ID dot title, and then dot check, matches, is selected. Let's run the test. Click on the green triangle. Yay! All of the tests passed. Just in case you are not sure whether the new test method has run, we can also do it by just clicking on the green triangle next to the method.
Then choose run. As you can see, the already favorite method was run, and it passed. In this chapter, we learned how to set up a hermetic test environment using test doubles and dependency injection. Next chapter, we are going to learn another technique: the robot pattern.
- Why test?
- Local vs. on-device
- Code coverage
- UI testing
- Hermetic environment
- Dependency injection
- Testing with MVP