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. :)

No comments: