http://daverog.wordpress.com/2009/12/14/why-android-isnt-ready-for-tdd-and-how-i-tried-anyway/
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).
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:
- Testing parts of the system that interact with the Android API
- 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;
becomesimport net.sf.json.JSONObject;
And because Android are using a slightly different version I need to change the constructor:
JSONObject json = new JSONObject(body);
becomesJSONObject 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.