Table of Contents

Unit Test of an Android Application using Robolectric

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.

Unit tests are programmed and performed whenever the code is modified to make sure that the new code does not disrupt current functionality. Unit tests are intended to verify the smallest feasible piece of code, including a function or method, and test it independently of the rest of the system. This enables developers to swiftly detect and resolve any problems early in the development process, boosting software quality and shortening the time needed for later 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.

Robolectric is a sophisticated tool that can help save time and effort while developing tests for Android apps. Robolectric is also a popular testing framework that allows users to perform unit tests for Android applications in a local JVM without requiring a physical or virtual device.

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.
  • It provides faster implementation as the test runs in a local JVM, there is no need to wait for an emulator or device to boot.
  • 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.
  • Unit testing with Robolectric can assist in finding errors and guarantee that the code works as intended.

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.

Explore More

Talk to an Expert

Subscribe
to our Newsletter
Stay in the loop! Sign up for our newsletter & stay updated with the latest trends in technology and innovation.

Our Work

Innovate

Transform.

Scale

Partnerships

Device Partnerships
Digital Partnerships
Quality Partnerships
Silicon Partnerships

Company

Products & IPs