Unit testing using Robolectric
Unit testing is a testing method where individual units of code are tested. A view or repository can be tested, for example, to check whether it meets the demands. Unlike most other tests, these kinds of tests typically are developed and run by a software developer.
Ideally, a test case is completely independent from other cases and other units. Since classes often depend on others substitutes such as mock objects needs to be used. In the previous recipe, the QuizRepository
class provides hardcoded quiz data (stubbed or mocked data), but as suggested, the intention is that the quiz data should be retrieved from a backend.
We are going to prepare the app we created in the previous recipe for unit testing, and we will create some tests ourselves. Robolectric is going to help us with that. Although since the 1.2 release of Android Studio unit testing (based on JUnit) has become much easier to set up, it still is not as powerful as Robolectric.
Robolectric does not...