Robolectric, Unit testing framework for Android
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:
- It provides a way to run our tests inside Android Studio, without launching app on Device or Emulator.
- 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?
- 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:
- 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
- androidTest
- main
- 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:
Here’s a breakdown of what’s happening in code:
- Configuration settings that can be used on a per-class or per-test basis.
- As Robolectric is based on JUnit, you have to set an annotation to indicate that RobolectricTestRunner.class will be performing the test.
- 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.
- Again, it is a JUnit 4 annotation. It’s the test that we want to run.
- Simply checks that our TextView exists or not.
- Checks that our TextView has the text “Hello Robolectric!”.
- Call this view’s OnClickListener, if it is defined. Performs all normal actions associated with clicking.
- It will create shadow object of Activity.
- 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.
- 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$
- 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: