Robolectric, Unit testing framework for Android

Umang Kothari
AndroidPub
Published in
4 min readMar 6, 2017
Source: Google

Why do we need?

Have you ever felt boring when you have to run your app for a small little change to see it is working fine or not?

If yes then, you must try Robolectric, it is a unit testing framework that allows Android applications to be tested on the JVM without an emulator or device.

These are the main features of it:

  1. It provides a way to run our tests inside Android Studio, without launching app on Device or Emulator.
  2. Shadow Classes, rewritten android core libraries by Robolectric, are the real magic of Robolectric. It is a replica of Android class with some exposed useful functions. Cool, isn’t it?
  3. You can test on Android components like:
  • Activity
  • Services
  • Broadcast Receiver

On app resources like:

  • Localization from strings.xml
  • Configuration like Landscape or Portrait
  • Styles and themes

And also for:

  • Multiple product flavors

How to integrate?

Let’s see, How can we integrate Robolectric in Android? There are just few steps:

  • Add following dependencies in Gradle:
Dependencies for Robolectric Framework
  • Now, we would like to add extra UI Button and TextView into our app’s main screen. Following illustrate how our XML codes looks like:
  • Initialize above views in MainActivity class like:

I know I know, it’s damn easy for you, you are doing everyday. But what if TextView’s text is not same as you have expected? What if Button’s tap leads to different screen rather than expected one?

Without running on emulator/device, how can we test it? For that, we have to create tests:

  • There are 3 folders reside at app > src directory named
  1. androidTest
  2. main
  3. test
  • We will add all our tests at test > java > [Your Package Name]. It’s standard practice to keep same project structure of both main and test folder. It will easy for us to track. Now create MainActivityTest class like this:
Robolectric Tests

Here’s a breakdown of what’s happening in code:

  1. Configuration settings that can be used on a per-class or per-test basis.
  2. As Robolectric is based on JUnit, you have to set an annotation to indicate that RobolectricTestRunner.class will be performing the test.
  3. It is a JUnit 4 annotation. Name itself suggests that this method should run before each test is run. We can initialize whatever objects are useful in entire class. Like, activity instance.
  4. Again, it is a JUnit 4 annotation. It’s the test that we want to run.
  5. Simply checks that our TextView exists or not.
  6. Checks that our TextView has the text “Hello Robolectric!”.
  7. Call this view’s OnClickListener, if it is defined. Performs all normal actions associated with clicking.
  8. It will create shadow object of Activity.
  9. Robolectric intercepts the loading of Android classes during testing. That’s why instance of shadow activity delegates to the application and return the next Intent on the started activities stack.
  10. Create Shadow Intent of next intent on the started activity.

I know, this is the simplest code in the entire universe. But, just think there are 4 buttons in a single screen. And each and every button’s onClick, there is complex logic and based on that logic different screen will be opened. More complex code is more error-prone. And that’s why we need tests. Now, it makes sense, right? ;)

How to run?

  • For Windows Users, The test artifacts feature is enabled by default, and the setting to enable/disable test artifacts can be found in File Menu -> Settings -> Build, Execution, Deployment -> Build Tools -> Gradle -> Experimental
  • For Linux or Mac Users, This can be accomplished by editing the run configurations, Defaults -> JUnit and changing the working directory value to $MODULE_DIR$
Edit Configurations for Linux/Mac Users
  • Open Gradle, Double click on ProjectName > :app > Tasks > verification > testDebugUnitTest:
  • Tests will be executed and results will be populated at app > build > reports > tests > testDebugUnitTest > index.html. Double tap on it. Test results be like this:
Test Summary

That’s it..! Now you can play with it…

You can check this sample app on Github.

Don’t forget to follow me on Medium and Twitter. If you liked the article, click the heart below so more people can see it! Thanks for reading!

--

--