Android testing

After a nice presentation introducing TDD on Android, I wanted to jump right in with LogMyLife. Of course, the very first thing I tried, I got a lovely error:

java.lang.RuntimeException: Unable to resolve activity for: 
Intent { action=android.intent.action.MAIN flags=0x10000000
 comp={net.sosiouxme.logmylife.activity/net.sosiouxme.logmylife.activity.Main} }
at android.app.Instrumentation.startActivitySync(Instrumentation.java:447)
[...]

I just love errors like this which pretty much just give me no idea what’s going on. But I got a clue from this StackOverflow question on the same error – when I followed the tutorial, I adjusted it for LogMyLife’s package structure, in which all Activities are in a sub-package (.activity) from the main one. So my constructor looked like this:

public MainTest() {
  super("net.sosiouxme.logmylife.activity", Main.class);
}

The tutorial doesn’t really say what these parameters are. Evidently the first one should be the package of the app (in this case, “net.sosiouxme.logmylife”), not the activity being tested; I guess this makes sense for the instrumentation to hook into. But it’s an easy mistake to make…

Next problem: one of the things I’m most interested in testing is my BroadcastReceivers, particularly the one that receives BOOT_COMPLETE (hard to attach a debugger in time to see that one go through). Oddly, while there are instrumentation classes for the other three major Android components, there’s none for a BroadcastReceiver. I asked about it on StackOverflow, we’ll see if there’s an answer (I seem to have a talent for asking questions that get no answer). Maybe this can just be mocked somehow?

Advertisements

One Response

  1. Bleah…

    Funny how the documented constructors won’t work at all!

    I poked around and haven’t found anything either specific to testing your broadcast receivers. I’ll bet we can hack something up for it though.

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

%d bloggers like this: