Android Application Testing Made Easy Using Robolectric

Every Android application, native or web browser based should have testing as an indispensable part of the project development. All aspects of the application from usability to dependency on external resources like GPS, SMS or network speed should be rigorously tested.

There are many testing frameworks available which can help perform an exhaustive functional, performance, regression and load testing of Android applications.

With this blog, I will attempt to outline one such framework called Robolectric.

As per, it is defined as: A unit test framework that defangs the Android SDK jar so that the developers can test drive the development of the Android app.

Some of the benefits of Robolectric that come from writing test cases while developing applications include:

  • Good unit tests that can help document and define the primary function of the application. It defines the scope and functionality of the application.
  • It helps deliver resilient loosely coupled and well fleshed applications.
  • Robolectric testing helps a developer in understanding the architecture and design of the product being developed. It comprehensively outlines the skeleton of the software, tests the functionality on parameters which define the expected behavior.

Configuring IDE for Building Test Cases

In the Robolectric testing framework, the Android Test Suites are based on JUnit 4. As defined by JUnit.Org: A JUnit test is a method whose statements test a part of the application under test. Developers organize test methods into classes called test cases (or test suites). Each test is an isolated test of an individual module in the application under test. Each class is a container for related test methods.

The best way to configure Eclipse for building test cases is to follow the installation guide available at Robolectric:

Download Jars:;quick~robolectric

Android Instrumentation vs. Robolectric

Testing API for Android provides a set of control methods often called ‘hooks’. These hooks form an integral part of the Android instrumentation API, thereby allowing control of the product lifecycle. It eventually controls how applications are loaded on Android. There is no way one can trigger callback methods like OnDestroy() using the Android framework API, something which can be achieved quite easily using  Android instrumentation.

Testing is typically slower under Android instrumentation as it relies extensively on packaging, dexing and installation of the Android application on a device/emulator running on Dalvik Virtual Machine Architecture.

Robolectric, on the other hand, is faster as it runs on the regular Java Virtual Machine (JVM). It does not require any dexing, packaging, deploying, etc. It runs outside the emulator of a JVM, not the Dalvik VM. I have experienced that test cases execute quickly in the JVM and execute slowly on the emulator.

Robolectric offers increased test coverage where not only non-android codes can be tested with ease but Android API specific codes can also be put to scrutiny.

How Robolectric Works?

Robolectric provides reference to the shadow objects representing the actual Android objects. These shadow objects represent the proxies of the real objects. Example, ShadowButton is a shadow object of Button class. The loading of Android classes is intercepted by Robolectric during testing and blinds shadow objects to the new Android objects. For instance, the Robolectric.shadowOf() method provides references to shadow instances allowing tests to assert on states.

Annotations Used:

To run the test case with Robolectric, annotate the test class using the @RunWith annotation.

public class LoginScreenTest {
  public void testingTheLoginFunctionality() throws Exception {
      // test code here

There are many other useful annotations which can be used while writing these test cases: @Before, @After, @withConstant, @Test. Some of these are defined in JUnit 4 package and Robolectric com.xtremelabs.robolectric.annotation package.

Using Code

ActivityController controller=Robolectric.buildActivity(LoginScreen.class).create(null);
Button login_button=(Button)loginscreen.findViewById(;
ShadowView shadow_loginbtn_view=Robolectric.shadowOf(login_button);
if( shadow_loginbtn_view instanceof ShadowButton){
/*Performs Button Click*/

The Robolectric represents the LoginScreen activity by the buildActivity() and returns a reference of the activity to be used in Test case project. Through the activity reference, we can obtain the reference of other UI elements and perform automated testing on them like, checkedPerformClick().

Useful Weblinks





Robolectric provides powerful ways to test mobile application not just for Android specific APIs but non-Android APIs as well. This allows developers to execute test cases outside of an emulated environment which results in faster execution of the test cases while developing Android application. The end result is that bugs are identified quite early in the development cycle thereby resulting in a robust product release. My verdict, as I conclude this blog is that automation testing on Android applications is effectively handled using Robolectric which ends up reducing the development time considerably.

Khushboo Kaur

Khushboo Kaur

Software Engineer II

Khushboo Kaur is a Software Engineer II at 3Pillar Global. Her responsibility in the company is to develop Android Mobile Applications. She is a Sun Certified Java Professional and has proficiency in Java Language ranging from web applications, standalone desktop applications to mobile applications on Java and Android mobile operating systems. She has worked on projects like CSR (bluetooth and music based app), LiveGuide (ticketing and barcode scanning application) and has experience on Mobile Automation. Prior to joining 3Pillar, Khushboo has worked with one of India’s largest VAS companies in the Telecom sector where she has developed several mobile applications .

One Response to “Android Application Testing Made Easy Using Robolectric”
  1. karthik on

    We have a requirement to run testcases which exteneds instrumentationtestcase using robolectric testrunner.
    is it possible to run, if it is possible tell me the approach otherwise suggest me alternative approaches.

Leave a Reply