Join Chiu-Ki Chan for an in-depth discussion in this video InMemoryFavorites, part of Effective Android Testing for Mobile Developers.
- [Instructor] In recipe activity, we use two functions from shared preferences favorites, get and toggle. Let's create an interface for that, so that we can provide an in-memory version during test. Right-click on data local, new, Java class. Call it favorites and change the kind from class to interface. Press okay. We will need to add the signature for get and toggle, so let's copy that from shared preferences favorites.
Copy the function signature in line 13 from Boolean to the parenthesis. Paste it in favorites and add a semicolon. Go back and do the same for toggle. Next, we will go back to shared preferences favorites and have it implement this interface. Implement favorites. Now that we have an interface we are going to create the in-memory version.
Go to the Android test package, right click on the package name, new, Java class. We will call it in memory favorites. The package name is going to be .data.local. Press okay. We will need to have in-memory favorites implement favorites, the interface. Then, press Alt + Enter to implement methods. Choose both get and toggle.
To implement favorites as an in-memory structure we are going to use a hash mark. In line three we will add private, final, map of the type string to Boolean. We will call it map and initialize it with a hash mark. Now that we have a place to store all these values, we can use it in the get method in line 11. We are going to try to retrieve some value for this ID out of our map.
Boolean value equal map.get ID. We are going to return false if the value is null, otherwise, we will return the value itself. Return value equal null, question mark, false colon value. This means that, if we have a stored value we are going to return whatever that is store. Otherwise, we will say that it's not a favorite because we couldn't find it in our map.
For toggle the logic is quite similar to what we did in shared preferences favorites. If you take a look there you can see that what we do is, in line 28, we first retrieve the value, and then we put it into the database as the negated value, and then return the negated value. So let's do that as well. In other words, we're going to implement put in in-memory favorites. Public void put.
This takes two parameters, the string ID and the Boolean value. We are simply going to put it into our map. So map.put with the ID and the value. Now that we have the put function, we can use it in toggle. Go to line 17, retrieve the value of our map. Boolean value equals map.get ID. In fact, we don't need to do it like this because we already implemented all the logic in the get function.
So, instead of map.get, we are going to use get directly, meaning the get function that we just wrote. Next we are going to put the negated value into our map. Map.put ID and then exclamation point value. Finally, we are going to return the negated value. So exclamation point value. We have two versions of favorites. One that is with shared preferences, and one that is in memory.
Next we are going to use dependency injection so that one is used in the app and the other one is used in the test.
- Why test?
- Local vs. on-device
- Code coverage
- UI testing
- Hermetic environment
- Dependency injection
- Testing with MVP