Introducing Robolectric
Robolectric (http://robolectric.org) is a unit test framework that intercepts the loading of Android classes and rewrites the method bodies. Robolectric re-defines Android methods so they return default values, such as null
, 0
, or false
. If available, it forwards method calls to shadow objects, mimicking Android behavior.
A large number of shadow objects are provided, but this is far from complete coverage, however, it is improving constantly. This should also lead you to treat it as an evolving open source project, for which you should be ready to contribute to make it better, but also to depend on it with caution because you may discover that what you need for your tests has not been implemented yet. This is not in any way to diminish its existing prospects.
Installing Robolectric
Robolectric can be installed by using the latest Robolectric JAR from the Maven central repository. At the time of this writing, the latest available is version 2.4:
testCompile 'org.robolectric...