androidservicetestcase

Android ServiceTestCase for IntentService


I'm currently writing unit tests for an android application and stumbled into the following issue:

I use the ServiceTestCase to test an IntentService like this:

@Override
public void setUp() throws Exception {
    super.setUp();      
}

public void testService()
{
    Intent intent = new Intent(getSystemContext(), MyIntentService.class);
    super.startService(intent);     
    assertNotNull(getService());        
}

However I noticed that my IntentService is created (means that onCreate is called) but I never receive a call into onHandleIntent(Intent intent)

Has anyone already tested an IntentService with the ServiceTestCase class?

Thanks!


Solution

  • I just got started into testing my own IntentService and it's proving to be a bit of a headache.

    Still trying to work things out but for the scenario where it seems that you do not receive a call to your method onHandleIntent(), (I'm not very good with the technicalities behind junit so forgive my use of terminology) it should be because the test framework, based on your code, actually tears down or end the test method once your call to startService returns. There is insufficient time for onHandleIntent to be triggered.

    I verified the above theory by adding an infinite loop within my test case - only then can I see my log statements in onHandleIntent logged.