Pippo's blog

it is all about software development

Moving to Robolectric 2

Android’s SDK is not a unit test enabler neither provide a fast way for developers to run unit tests. It is difficult to write and hard to run.

Robolectric fills a gap on android testing, more specifically on unit testing, that allows developers to get fast feedback and enables TDD. TDD is a hot topic these days and some of the concerns is when it comes to mocking.

By using RobolectricTestRunner class, developers get the benefit of having test doubles for android’s base classes and they can quickly run tests on the JVM, no need for the Dalvik’s VM.. This is true since version 1 of the framework although we still needed to many mocks to accomplish a test “mission”.

After the 2nd version of the framework, the embeded test doubles are much more realiable, mimicking device’s behavior much closer to our expectations and thus allowing for less mock dependent tests.

If you are in a project that uses Robolectric version 1, don’t be afraid of upgrading to the new version, it has many benefits. Keep in mind the library is not backwards compatible and adjustments will be required. The good news is that you can start having the benefits of the new version without having to migrate all your tests at once.


The versions have different package naming structure, which means they are basically 2 different libraries and as so, you can add both to your project at the same time. Is that a good idea? Probably not for the long run, but it definitely is for the migration phase, which can last for a few weeks.

Pay attention to what packages to import when writing new tests and you will be very happy with the decision. If you have apklibs on your project, also keep an eye on the project.properties file, as the framework use it for reference and it can be the source of strange problems.

After that, just keep improving your test suite and remember how a test pyramid should look like.