Why Android isn’t ready for TDD, and how I tried anyway

Pick a station to find out more...

Over the last few weeks I’ve been attempting to develop an small application for the Android development platform. In particular, I’ve been applying the techniques of test-driven development (TDD) to this project in an attempt to understand whether they are compatible with the platform. But before launching into more a more detailed explanation of the problems I’ve faced I will provide a brief description of the app I have written…

As an avid listener of BBC radio, particularly 6music and Radio 4, I always want to find out about what I’m listening to. I find the text provided by my DAB radio is both limited and slow. Sometimes it says something like “Why not tell us what you’d like to listen to blah blah” (or similar generic message, and not the currently playing track) which takes forever to scroll across my 30 character LCD display. The solution? It’s called ‘BBC Radio What’s On': on your Android phone you select from a list of available radio stations (see above).

What's on 6music?

Then you are shown a page with information about:

1) What programme is on now
2) What’s on next
3) What were the last 10 tracks played on this station*

*This data is only as accurate as the data provided by the BBC radio teams, which vary from show to show

So, as you can see, a very simple application, but (arguably) very useful. As far as I am aware, no BBC web page provides this data in one place. The nearest to this is the new configurable BBC mobile homepage. This allows the user to select radio stations to appear on the homepage indicating what show is on now, but no information about current tracks.

Development

Most people I have spoken to about Android development have been broadly positive. The speed and ease with which you can create and deploy a basic application on the emulator or a real phone is impressive. The tight integration with Eclipse makes development simple (performance issues aside, see below). Where I really found development problematic was when attempting to continue developing in the way I do when building web applications (i.e. using test-driven development and dependency injection).

Why testing is a problem

A basic unit testing scenario is that I want to test some code that makes use of a third-party utility library. In my application I wanted to make a service that reads a JSON file from a URL (using my HttpService) and returns a object representing the JSON in way that reflects the domain. This object is a RadioStation object which contains meta-data about a particular radio station. The method updateRadioStation(radioStation) takes a radio station and populates the fields based on JSON. My test, shown below, is trying to capture this functionality:

@Mock HttpService httpService;

@Test
public void loadsDataCorrectlyFromJSON() throws IllegalStateException, IOException, JSONException {
    RadioStation radioStation = new RadioStation("x", "service", "radio x");

    when(httpService.callUrlAndReturnString(anyString()))
        .thenReturn(FileResources.BBC_RADIO_STATION_JSON);

    radioStationLoader.updateRadioStation(radioStation);

    assertEquals(radioStation.getCurrentShow().getType(), "On Now:");
    assertEquals(radioStation.getCurrentShow().getTitle(), "Cerys on 6");
    assertEquals(radioStation.getCurrentShow().getSubtitle(), "16/10/2009");
    assertEquals(radioStation.getCurrentShow().getPid(), "b00n9nfv");
    assertEquals(radioStation.getCurrentShow().getSynopsis(), 
                 "Music, chat and more archive sessions with Cerys.");
}

Unfortunately, if we run this test as a junit test, we get the following error:

java.lang.RuntimeException: Stub!
    at org.json.JSONObject.(JSONObject.java:35)
    at daverog.bbcradio.service.RadioStationLoader.updateRadioStation(...:29)
    at daverog.bbcradio.service.RadioStationLoaderTest.loadsDataCorrectlyFromJSON(...:36)
    etc...

The problem here is that the android.jar supplied with the SDK is stubbed out with no implementation code. The solution that seems to be expected is that you should run your unit tests on the emulator or a real phone. Google have provided all the tools to do this; in fact it is an intrinsic part of the platform. To run a test on the emulator, the Eclipse plugin supplies a new run target ‘Android junit test’ (or simply install the application as normal and all unit tests will be run). This will load up the emulator, load the whole application onto the device, and run the test inside the Dalvik virtual machine*.

Dalvik virtual machine
From Wikipedia, the free encyclopedia
Dalvik is the virtual machine which runs the Java platform on Android mobile devices.
It runs applications which have been converted into a compact Dalvik Executable (.dex) format suitable for systems that are constrained in terms of memory and processor speed.
Dalvik was written by Dan Bornstein, who named it after the fishing village of Dalvík in Eyjafjörður, Iceland, where some of his ancestors lived.

By running on the emulator and against the fully working Dalvik-compiled platform the unit test will succeed. Well, it would succeed, if you’d done the following:

  • Made all tests extend some kind of Android test case class (e.g. AndroidTestCase)
  • Stopped using a mocking framework
  • Downgraded to junit 3
  • Tweaked the imports to match Android’s package changes (e.g. framework.junit from org.junit)

and worst of all?

  • Wait at least 10 seconds for even a single simple test to run

The five points above make testing on the emulator a largely impractical process. Testing needs to be simple and responsive, and developers need to be able to leverage powerful matching and mocking frameworks. The rest of this blogpost assumes that the above five points hold true, so I should briefly explain some of the alternatives I tried or thought of that did not work:

1) Import a mocking framework (e.g. mockito) into the project as an additional dependency.

Any imported jars containing class files not compiled to Dalvik bytecode (most) will not work. Attempting to compile the source along with your project will not work either because most libraries will make extensive use of parts of the Java language not compatible with Dalvik: it uses its own library built on a subset of the Apache Harmony Java implementation.

2) Obtain a version of the android.jar where the utility libraries are not stubbed.

Tried and failed, but please tell me if you find one.

That the deployment environment (Dalvik) can place restrictions on the way in which code is tested is very frustrating. For the same reason, we also have a restriction on the libraries we can use as part of the application. The Android platform provides a very extensive array of libraries that make it easier to write most applications, particularly those with a web-focus. However, I feel one is missing that is central to creating applications using test-driven development and dependency injection: Spring. The inclusion of a lightweight IOC container would be an excellent addition to the Android platform.

I have one final bugbear that I need to address. It is not important because it will be possible to fix, but it caused me more frustration than any other part of the development process. I am referring to the performance of Eclipse when using the Android SDK plugin. When Eclipse first loads and you are working with an Android project, the responsiveness is quite acceptable, but, for whatever reason, Eclipse increasingly slows. Use of the emulator is particularly detrimental and after around half an hour of development it can take 10-20seconds to switch between two file tabs. This is unacceptably slow and I find myself rebooting Eclipse and the emulator every half hour to alleviate the problem. Moan over.

An approach to TDD on Android

Based on the assumption that running tests on the emulator is too restrictive and slow, I wanted to come up with an approach that at least partially facilitates test-driven development. First, in Eclipse, we create a new Java (not Android) project call [ProjectName]Test which will be dependent on the main project and hold all the tests and test resources. By having a separate non-Android project we can add resources and code that do not conform to the Android project specification and satisfy the automatic validation. This project will be setup to use a standard JRE system library. Now we can add tests to this project that test the classes in the main project.

The first general suggestion I’d make is: keep things simple by trying to make as many services as possible not dependent on the parts of the Android platform that are not compatible with a conventional JVM. This avoids the “Stub!” error I mentioned above. The LastFmRssReader service does not use any Android-specific libraries so it can be tested using whatever testing or mocking frameworks you wish:

public class LastFmRssReader {

    ...

    private RssFeedParser parser;

    public List getPlayedTracksForLastFmUser(String lastFmUser) {
        parser.setUrl(HTTP_WS_AUDIOSCROBBLER_COM_1_0_USER+lastFmUser+RECENTTRACKS_RSS);

        List playedTracks = new ArrayList();
        List messages = parser.parse();

        for(Message message:messages){
            String title = message.getTitle();
            String artist = "";
            ...
            playedTracks.add(new PlayedTrack(artist, title, message.getDate()));
        }

        return playedTracks;
    }

}

an example test:

@Test
public void readerCallsCorrectURLAndExtractsArtistAndTitleFromMessageTitle(){
    LastFmRssReader lastFmRssReader = new LastFmRssReader(rssFeedParser);

    ArrayList messages = new ArrayList();
    Message message= new Message();
    message.setTitle("artist - title");
    messages.add(message);        

    when(rssFeedParser.parse()).thenReturn(messages);

    List tracks = lastFmRssReader.getPlayedTracksForLastFmUser("testuser");

    assertEquals(tracks.get(0).getArtist(), "artist");
    assertEquals(tracks.get(0).getTitle(), "title");

    verify(rssFeedParser).setUrl(
        "http://ws.audioscrobbler.com/1.0/user/testuser/recenttracks.rss");
    verify(rssFeedParser).parse();
}

This approach should work well, particularly for larger applications where more and more services perform an internal role where external libraries are not required. It’s also the best way to test domain objects which usually don’t need mocked services. Where this approach fails is where much of the code written is either using libraries or the parts of Android API, which would be common in small applications (such as ‘BBC Radio What’s On’).

Using the approach above, two problems remain:

  1. Testing parts of the system that interact with the Android API
  2. Testing parts of the system that use common libraries embedded in the Android platform

I am stumped regarding the best way to test code that uses the Android API. I have been reverting to testing on the emulator using Junit 3 and no mocking framework. The android.test.mock package may help with this, but seems to be just a bunch of stubbed out implementations for you to override as you require, so does not offer nearly as much power as a mocking framework.

I have found an unconventional way of testing with the common libraries embedded in the Android platform. This approach is the best example of how awkward testing can be on Android.

If we go back to the test I first used as an example:

@Mock HttpService httpService;

@Test
public void loadsDataCorrectlyFromJSON() throws 
        IllegalStateException, IOException, JSONException {
    RadioStation radioStation = new RadioStation("x", "service", "radio x");

    when(httpService.callUrlAndReturnString(anyString()))
        .thenReturn(FileResources.BBC_RADIO_STATION_JSON);

    radioStationLoader.updateRadioStation(radioStation);

    assertEquals(radioStation.getCurrentShow().getType(), "On Now:");
    assertEquals(radioStation.getCurrentShow().getTitle(), "Cerys on 6");
    assertEquals(radioStation.getCurrentShow().getSubtitle(), "16/10/2009");
    assertEquals(radioStation.getCurrentShow().getPid(), "b00n9nfv");
    assertEquals(radioStation.getCurrentShow().getSynopsis(), 
        "Music, chat and more archive sessions with Cerys.");
}

This test cause a “Stub!” exception when run as a unit test.

Here’s the implementation I am testing (it’s loading json from a URL and populating a domain object):

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class RadioStationLoader {
    ...

    public void updateRadioStation(RadioStation radioStation) throws 
            IllegalStateException, IOException, JSONException{
        String url = HOST + radioStation.getBrandId()
                + "/programmes/schedules" + radioStation.getServiceTypeURLExtension()
                + "/upcoming.json";

        String body = httpService.callUrlAndReturnString(url);

        JSONObject json = new JSONObject(body); <--- This is where the exception is thrown

        JSONObject schedule = ((JSONObject) json.get("schedule"));

        JSONObject service = ((JSONObject) schedule.get("service"));
        String serviceTitle = service.getString("title");

        radioStation.setName(serviceTitle);

        JSONObject now = ((JSONObject) schedule.get("now"));
        JSONObject currentBroadcast = ((JSONObject) now.get("broadcast"));
        JSONObject next = ((JSONObject) schedule.get("next"));
        JSONArray broadcasts = ((JSONArray) next.get("broadcasts"));
        JSONObject nextBroadcast = ((JSONObject) broadcasts.getJSONObject(0));

        radioStation.setCurrentShow(extractRadioShowFromBroadcastJson(
            "On Now:", currentBroadcast));
        radioStation.setNextShow(extractRadioShowFromBroadcastJson(
            "On Next:", nextBroadcast));
    }

    private RadioShow extractRadioShowFromBroadcastJson(
            String type, JSONObject broadcastJson) throws JSONException{
        String start = broadcastJson.getString("start");
        String end = broadcastJson.getString("end");
        JSONObject programme = ((JSONObject) broadcastJson.get("programme"));
        JSONObject display_titles = ((JSONObject) programme
                .get("display_titles"));
        String title = display_titles.getString("title");
        String subtitle = display_titles.getString("subtitle");
        String pid = programme.getString("pid");
        String synopsis = programme.getString("short_synopsis");
        return new RadioShow(type, title, subtitle, start, end, synopsis, pid);
    }

}

If I copy this class into my test project and locate in the same package then I can run against a different test copy. What I want to do now is run the code against an implemented version of the JSON library. If I import the JSON jar (JSON lib) into my project along with its dependencies then I can use this instead of the Android stubbed version. Because the package structures between the external library and the Android version are different I need to switch the import to use the alternative library:

import org.json.JSONObject;
becomes
import net.sf.json.JSONObject;

And because Android are using a slightly different version I need to change the constructor:

JSONObject json = new JSONObject(body);
becomes
JSONObject json = JSONObject.fromObject(body);

The class then compiles. Running my test is now successful:

This approach allowed me to quickly find several bugs in the RadioStationLoader before copying over the fixes to the original and deploying to the emulator. This is clearly not a sustainable approach but it provides a solution to developing code in a test-driven, dependency injected way.

Finally, I needed a way to wire my services together to provide them to the main Activity classes. It’s hard to write applications using inversion of control without somewhere to invert the control to. In Java web applications this would be Spring . In Android, no such framework is available. This seems unfortunate as Spring (core) is lightweight (in many senses), even more so with a stripped-down alternative like Spring ME. Without such a framework, a developer is limited in leveraging the power of IOC architectures. My crude alternative was to configure my system and inject my dependencies in an ‘application context’ Java class.

client = new DefaultHttpClient();
httpService = new HttpService(client);
radioStationLoader = new RadioStationLoader(httpService);
rssFeedParser = new RssFeedParser();
lastFmRssReader = new LastFmRssReader(rssFeedParser);

public RadioStationLoader getRadioStationLoader(){
    return radioStationLoader;
}

public LastFmRssReader getLastFmRssReader(){
    return lastFmRssReader;
}

finally…

The one main recommendation that I would make as a result of this work would be for Google to provide an alternative android.jar that is stubbed as little as possible. This is particularly important where common open-source libraries have been integrated into the platform. This would make unit testing, and therefore test-driven development, much less painful. Oh, and Spring on Android would be nice too.

About these ads

14 thoughts on “Why Android isn’t ready for TDD, and how I tried anyway

  1. Pingback: Pigsaw Blog » Blog Archive » Bookmarks for 15 Dec 2009

  2. I feel your pain – no really, I have just gone through the same damn thing. First serious flaw in the android platform that I have run across really.

    As for IOC dependency injection, have you looked at Guice and RoboGuice these seem to work very well and appear to be completely cross platform. I have tests running in the JVM for an android that’s running RoboGuice and Guice.

    • I had subsequently come across Guice, although it’s good to hear you’ve used it on Android (hopefully) without major issues. Thanks for pointing out RoboGuice, it looks like an excellent project and one which I’ll seriously consider for my next Android project.

  3. Excellent blog entry, thanks! Both good and sad to hear that other developers are facing the same problem when trying TDD with Android. I consider this a serious impediment to developing on Android, so let’s hope more solutions become available soon…

  4. Wow, amazing parallels to a couple projects I worked on in the last year. Because of all the pain you identified above, we ended up creating Robolectric, an open-source in-JVM Android simulator for making TDD fast without the pain of segregating off your Android code or spiraling into total dependence on mocks. Check it out. Sub-second test runs FTW!

    • Hi Christian, thanks for the comment. Robolectric looks awesome, I’ll be passing the details on the mobile development team at the BBC and definitely giving it a go in my next Android project.

      After looking around the website http://pivotal.github.com/robolectric/ I did notice something that I thought it would be worthwhile commenting on. It reads:

      “The use of mock frameworks can make tests hard to read and understand. Mocking causes tests to become reverse implementations of the production code.”

      Whilst I would agree that reverse implementations are a potential outcome of mocking frameworks, I see no other way to provide tests at the level of a programming ‘unit’. The unit will interact with other services, if these are not mocked, the testing is being applied to a integrated set of services rather than the unit.

      If I have understood Robolectric correctly, I would see it not as an ‘alternate’ to mocking frameworks, but as an essential complement to mocking frameworks, where Robolectric provides integrated testing and mocking frameworks allow isolation at the unit level.

      Putting this niggling criticism aside, Robolectric looks very cool.

  5. My approach to TDD on Android:
    MVP architecture + Mockito.
    Create MVP troika in onCreate.
    The Activity only forwards calls to unittestable POJO’s.

    Don’t mock types you don’t own (in this case Android!) instead wrap all used Android classes.
    Worked well for a small app, but as requirements grew I found myself facing wrapping of almost every Android class, and gave up. Back to using the Android testclasses…

  6. I agree with a lot of what is said above, particularly about using RoboGuice. On the subject of IDEs, have you tried using IntelliJ Idea instead? The android support is very good and it is free (as of version 10). I haven’t come across any of the performance issues you mention.

  7. Excellent blog entry. I have found lots of similar things in other module/apis from Google for Android.

    For instance, Google’s own Java GData apis to talk with YouTube, Google Mail, Docs, etc don’t work on Android. Also their apis for dealing with SQLLite are very very kludgy and unpolished. They tend to not fully think through their public apis to make development simpler.

    On another note, I am sure Roy at SpringSource who is running the Spring Android project must be looking into creating a lightweight version of Spring to run on Android. At the moment, there is really just the RESTTemplate in the Spring Android project. However, I am working on something special to add to the project, that I hope they will accept. I’ll just say, I plan on correcting Google’s poor api for SQLLite, in a Spring style.

  8. I had some luck placing the data/layoutlib.jar in my classpath before android.jar, e.g. {sdkhome}/platforms/android-12/data/layoutlib.jar. That has at least a Uri implementation (what I was after) that isn’t stubbed.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s