Testing activities and applications
Here, we cover some common cases that you will find in your day-to-day testing, including dealing with Intents, Preferences, and Context. You can adapt these patterns to suit your specific needs.
Mocking applications and preferences
In Android parlance, an application refers to a base class used when it is needed to maintain a global application state. The full package is android.app.Application
. This can be utilized when dealing with shared preferences.
We expect that the tests that alter these preferences' values will not affect the behavior of the real application. Without the correct testing framework, the tests could delete user account information for an application that stores these values as shared preferences. This doesn't sound like a good idea. So what we really need is the ability to mock a Context that also mocks the access to SharedPreferences
.
Our first attempt could be to use RenamingDelegatingContext
, but unfortunately, it does not mock SharedPreferences...