Tuesday, April 28, 2009

Testing your Android applications

For future reference, Můj Android.cz will probably be a good blog to look at for test-driven Android development. The linked article is the only one so far though, and the rest of the blog seems to be in Czech (?). But this quote is particularly pertinent:
"There are basically three levels of tests/unit-test you can use in your application:
  1. Unit testing of logic which does not depend on android at all. This is similar to every usual unit testing. Running tests directly from Eclipse requires some configuration changes which we will cover later.
  2. Unit testing of business logic which depends on Android but does not depend on Android application elements and ui. These logic does not require activity to be running with complete context and it can be tested in isolation from ui. These tests usually require something from context (e.g. resources, configuration, logger, some native classes)
  3. Unit/funcional testing of Android application elements. These tests are fully instantiated activities, services, content providers and applications. Via instrumentation it is possible to send keyboard and touch events to the activities and check response in ui. It is possible to test lifecycle of a service and to test databases changes made by content provider."
I've been trying to figure out how to test the second case, since I've been getting "Java RunTimeException: Stub!" errors for a while. As it turns out, you cannot just use JUnit to test code that calls on Android APIs: "android.jar in the SDK is only stubbed methods/classes, and contains no code." Even if you are not testing an Activity or any UI elements, you will still need to use Instrumentation. Once I figure this all out, and I am not lazy, I hope to write a very simple tutorial. Unless someone beats me to it, in which case I won't. :)

Monday, April 27, 2009

Android ApiDemos in Eclipse

I've been learning how to write Android apps lately, and I've been trying to figure out to use TDD. Diego Torres Milano's blog has been pretty helpful to me, especially his post about how get Android ApiDemos' tests to work. As a newbie to Eclipse and Android development though, I had some trouble with his instructions. Here are a few things I did that I didn't think was very clear on Diego's blog:
  • Create an ApiDemos Project first (New Android project -> Project from source -> use the source from [android SDK directory]/samples/ApiDemo).
  • Don't bother to delete the Dummy class or remove the source directory you created (when you follow his instructions to create the ApiDemosTest project).
  • The emulator has the ApiDemos installed by default. You should uninstall the default installation (adb uninstall com.example.android.apis) and run your ApiDemos project in Eclipse. If you don't, you'll get errors when you run ApiDemosTest because the default ApiDemos installation is in a separate security domain (I think).
Other than these points, his post (and the comments) are pretty clear. This entry from Zhao's Weblog is also helpful.