Tuesday 16 June 2009

Making a mockery

My Android development continues well and I've started work on my second app, with plans for a free version and a paid for version ( around the $1-2 mark ). Being a seasoned dev I decided that this one should have automated testing. Unit tests for each class, and an automated testing system for putting it through it's paces on the emulator.

Now I expected the second one to cause problems and I was quite prepared for there to not be any automated testing systems for Android, at least not at that level. What I wasn't expecting was the difficulty of unit testing. Android (in 1.5 at least) has several utility classes for testing. Theres a ActivityUnitTestCase which seems to be the android equivalent of TestCase in JUnit. Then theres ActivityInstrumentationTestCase which seems to be a mechanism actually launching and manipulating an activity inside of either the emulator or an actual device.

The problem is both of these requirement me to add something to the device or emulator, some kind of instrumentation package. Yes , thats right - to the device. Acceptance testing, ok thats unavoidable but unit testing ? Come on, I shouldn't have to run my unit tests inside an emulator.

I want to run my unit tests inside the IDE or inside an ant task . I should be able to mock out Android classes and just test how my code interacts with the mocks. Why can't I do this (using the excellent easymock) :

expect( MockActivity.findViewById(R.id.someButton) ).andReturn( mockButton );

One word : Dalvik. The Android plugin is compiling all the code to dalvik bytecode and the only dalvik jvm on my machine is inside my emulator. Running it inside Eclipse doesn't work and never will.

Thanks google, I'm sure an awful unit testing setup wont make your market apps worse.