Unit Test your Android application using Robolectric
Submitted by Leena S N (@leenasn) on Monday, 30 September 2013
Section: Workshops Technical level: Intermediate
This workshop is for giving an overview on how to write faster, maintainable and behavioural tests using Robolectric, an Android Unit Testing Framework. At the end of the session, the attendees should be able to:
- Learn about basic Android Unit Testing and understand the issues with the default AndroidTestRunner
- Overview of Robolectric
- How to write tests using Robolectric and run it in JVM [i.e. instead of Dalvik]
- How to integrate Robolectric tests with your Continuous Integration Server such as Jenkins
Android SDK comes with a testing framework called Android Instrumentation Runner which is written on top of JUnit. The problem with it is that it is slow as it requires dexing (i.e. converting into Dalvik Executable), packaging and installing on the actual device or emulator. This can become a bottleneck as unit tests are expected to run fast.
Running the tests in the JVM is not possible by just mocking because, the actual class definitions are available only in Dalvik and not in android.jar which the SDK provides. The android.jar throws RuntimeException for all the methods, and its tough to mock or extend because of the way the Android SDK is designed.
Robolectric on the other hand has rewritten Android SDK classes so they can run in the JVM. It also handles inflation of views (i.e. creating views from the xml file), resource loading, and lots of other stuff that’s implemented in native C code on Android devices. This allows tests to do most things you could do on a real device. It’s easy to provide our own implementation for specific SDK methods too, if needed.
The workshop is intended for anyone who wants to write Android Unit Tests as FIRST (Fast, Isolated, Repeatable, Self-Veifying and Timely)
- Wisdom wise:
- Experience with Android Development
- Familiarity with TDD will be an added advantage
- Hardware wise:
- You have to bring your own laptop (don't forget the charger)
- Please bring your own Android device to test out your apps on them in real time
- Make sure your machine configuration is reasonable enough to withstand the dev environment.
- Software wise:
- Operating System: Windows XP (32bit), Windows 7 (32 /64 bit), Windows 8 (64 bit), Mac OS X 10.5.8 or later (x86 only) or Linux (tested on Ubuntu Linux, Lucid Lynx)
- Eclipse IDE (or Participants can directly download platform specific ADT Bundle from http://developer.android.com/sdk/index.html)
- Eclipse 3.6.2 (Helios) or greater
- Note: Eclipse 3.5 (Galileo) is no longer supported with the latest version of ADT.
- Eclipse JDT plugin (included in most Eclipse IDE packages)
- JDK 6 (JRE alone is not sufficient) with latest update needs to be installed
- Android Development Tools plugin (recommended)
- Preferred Android SDK version is 4.2.2 i.e. API Version 17
- Note: Specific libraries required for the workshop will be shared during the workshop.
Krishnaprasad, alias KP, is the Senior Software Architect @ Multunus. His technical expertise ranges from J2EE, Ruby On Rails on the Webapp technologies to Android Native and Hybrid apps. He is a big fan of TDD too. He has also spoken at Droidcon India 2011. He is all set to educate the world with his knowledge about the Android universe.