Join Chiu-Ki Chan for an in-depth discussion in this video UI-less on-device test: Lookup test, part of Effective Android Testing for Mobile Developers.
- [Instructor] We also want to make sure we can retrieve a recipe by its ID. Copy and paste the count method from line 20 to line 27. Rename it get chocolate pudding. Delete the assert lines. If you look at the recipes folder, we have a recipe called chocolate pudding, and its ID is chocolate_pudding. We are going to try to retrieve it in our test.
To retrieve this chocolate pudding recipe. We are going to call store.getRecipe. Recipe, recipe, equal store.getRecipe and then with positive string chocolate_pudding. The getRecipe function is not yet written, but we can generate from the test. Press alt enter, and choose create method getRecipe. It will have to return type recipe and then it will get a perimeter string, but instead of calling it chocolate pudding, we'll call it ID.
The default implementation just returns null. But we don't want that. We want to be able to actually look up this recipe. Right now, we don't have a look up table. So lets add that. Go to the top of the file and create a private HashMap on line 16. Private final map it will be from, type string to recipe. And we'll name it map.
Initialize it with a HashMap. Scroll back down to the get recipe function. In line 62, change the return value from null to map.get ID But we are not done yet. Right now the map is empty. So whatever ID you pass it, is going to return null. To populate this map, we need to scroll back up to the point where we are generating the recipe out of the files and the streams.
Go to line 26. Right after we add the recipe to our list of recipes, we will also need to add it to our map. So let's add map.put. The key will be recipe.id and then the value will be recipe. Let's go back to the test and verify that we are retrieving this recipe correctly. First, let's verify that this recipe is not null.
Assert not null recipe. Run the test. The test passed. Next, we are going to verify the fields inside this recipe. First, let's check that the ID is what we expect. Assert equals, and what is the ID? Let's look at the file directly. So go to chocolate_pudding.txt and ID should be chocolate_pudding.
Back to the test. Assert equals chocolate pudding and then recipe.id. Let's run the test. Great, that works. For the title, we are going to press command D to duplicate the line, and then change recipe.id to recipe.title. The title of this recipe is chocolate pudding with capital letters and a space in between.
Run the test. We can use the same technique to verify the description, but we are not going to do that right now in this course. There you go. We have just written an instrumentation unit test to verify that we can read the recipes from the asserts directory. Because of the extra time needed to push it to a device, in general, it is better to write JVN unit test instead of instrumentation unit test.
It requires some thinking to separate the pure Java part of your app from the android part. But once you get used to it. It is a great way to write your app and run tests quickly to add more logic.
- Why test?
- Local vs. on-device
- Code coverage
- UI testing
- Hermetic environment
- Dependency injection
- Testing with MVP