Unit Test of an Android Application using Robolectric

 In Blog, Software & Mobile Application

Unit Test of an Android Application using Robolectric

Have you ever hit the ‘run’ button in Android Studio, and then have waited for quite some time for the Android emulator to generate, load, and build your project? Testing libraries and frameworks are designed to address this problem. Robolectric Framework makes unit testing for Android applications, faster and more reliable.

What is Unit Testing?

Unit testing is a method where individual units of software are tested. The purpose is to validate that each unit of the software code performs as expected.Unit testing is a level of software testing where the developer tests a particular function or method of the software to check if it performs as designed. A unit is the smallest part of the software that can be tested and software developers usually perform this type of testing.

Android Application Unit Test

Android unit tests are written in Appium or instrumental test, and  are run on an Android emulator or an Android device, which is too slow to build, deploy, and run.

To overcome that challenge, there is a better way to write unit test for Android applications, which is using the Robolectric framework.

What is Robolectric Framework?

Robolectric Framework makes unit testing for Android applications, faster, and more reliable.

Robolectric provides a JVM compile version of the android.jar file. Robolectric handles views, resource loading,  and many other things that are implemented in the Android native.

This enables you to run your Android tests in your development environment, without requiring any other setup to run the test. Robolectric supports resource handling, e.g. views. You can also use the findViewById() to search in a view.

Robolectric framework provides the Shadow Objects that represent the Android classes. Shadow Object has behaved similar to the Android implementation.

Example

Take an example of testing an Android application using Robolectric Framework.

First, you need to add dependence to Gradle.

XML

activity_one.xml


activity_two.xml

Java

OneActivity.java


TwoActivity.java

Test


Above @RunWith annotation indicates that the test run with RobolectricTestRunner Environment, and @Before annotation, itself suggest that the method should run before each test, and that with method we can initialize activity instance.

Here, @Test annotation represent test, in the

  1. I have checked the text of TextView using acivity instance created in setUp() method.
  2. Same as the above test, but after the button click event perform check the text of TextView.

Above test I have used the Shadow Object of activity, using that call getNextStartedActivity() that return next Intent on started activity. Then Create Shadow Intent of next intent on the started activity and last statement is to compare both intent by that class name.

Features of Robolectric

  1. Provide way to run test android application inside the android studio.
  2. Provide Shadow Classes that represent the core library of android.
  3. Using Robolectric you can test like:
  4. Activity
  5. Services
  6. Custom View
  7. Broadcast Receiver

Advantages

  • Unit testing does not require an emulator for execution, which makes unit testing faster and easy.
  • With Robolectric, you can quickly run a single test within an Android studio while developing.
  • Robolectric can fake almost every Android-related thing as Shadow Objects.
  • Shines when testing multi-threaded code like AsyncTasks, Loopers, and Handlers, etc.
  • We can also combine other java testing modules with this like Mockito, PowerMock, etc.

Disadvantage

  • Robolectric does not support all the functionalities of real devices like Sensors, OpenGL, etc.
  • For integration test, read device is required.

Possible test on above example

We can also add one more test to the above example, which is whether or not the activity change is performed after the button click.

Conclusion

Looking at the above points, we can state that Robolectric framework is an apt tool to test an android application within the Android studio. We can do much more than above example with Robolectric framework. Once mastered, you can significantly improve the quality of your code. To know how to get started, get in touch with our team.

Role of Edge Computing in Connected and Autonomous VehiclesApplying DevOps to Improve the Effectiveness and ROI of IoT Testing