December 17, 2012   |   2min read

Android Testing - The Next Generation

Robospock: The Next Generation of Test Automation for Android

Robospock enables superfast Android unit and integration testing using Java Virtual Machine as a platform. If you do any kind of serious Android development then take a look, this test platform MAY be the one you’re looking for!

Modern Java testing

When it comes to testing our Java code, we try to use the most advanced tools that are out there. We recently fell in love with Groovy and Spock testing frameworks. Testing Java with Java though is a pain in the you-know-what. Therefore our test stack supports the creation of readable test cases, which also provide code usage examples and are extremely helpful for other developers. Here’s a snippet showing how to understand interactions with tested object thanks to use cases:

def 'user registration saves user in repository'() {
  def repository = Mock(UserRepository)
  def registrator = new UserRegistrator(UserRegistrator)

  registrator.register(new User())

  1 * repository.storeNewUser(new User())

Thanks to this simple and clear specification we get a better understanding of how the repository should work.

Android and unit tests

There are a number of Android test frameworks, not many of them running locally on the JVM which is crucial if you work TDD. If your tests need some warming up time and take lots of time to execute (e.g. more than 30 seconds) let’s face it, you won’t be willing to run them every time you change a small bit of your code and therefore you won’t follow TDD at all. Fortunately there’s a really good tool at hand: The Robolectric test framework, which uses classloader runtime manipulations to mock most of Dalvik VM classes and makes it possible to run Android code on the JVM. We’ve been using Robolectric for quite a while now and enjoyed most of it. Unfortunately it is tightly bound to JUnit and the work to separate concerns in the framework was quite a story thanks to which we’ve not only ported Robolectric to Spock but also understood what’s under the hood of Robolectric. It was worth it.


May I present the most modern test combination for Android code - Robospock!. Robospock makes working with Robolectric a real pleasure so let’s take a look at code sample of Android specification.

def "should display hello text"() {
    def textView = new TextView(Robolectric.application)


    textView.text == "Hello"

Seems easy? It is. Well yes, but this is only thanks to the Robospock library, which connected some hard classloading work with the absolute magic of Spock. In other words, you get the best of both worlds. Now that’s logical, Captain. Visit our project page to see more details and use examples.

Wojtek Erbetowski

Head of Engineering

Did you enjoy the read?

If you have any questions, don’t hesitate to ask!