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!
You just have to add a:
Choose the
XXXX
after the startService, then it will let the thread go into theonHandleIntent
method.In Android Studio 1.1, when running tests using the Run/Debug Configuration | Android Tests facility on any unit under test code (UUT) that extends IntentService, the ServiceTestCase.java (JUnit?) code does not call onHandleIntent(Intent intent) method in the UUT. ServiceTestCase only calls onCreate so the problem is in the test code.
In my file smSimulatorTest.java:
At this point, I'm looking for other solutions in the testing framework that test UUTs through Intents since this is how IntentService is instantiated.
http://developer.android.com/reference/android/app/IntentService.html - To use it, extend IntentService and implement onHandleIntent(Intent). IntentService will receive the Intents, launch a worker thread, and stop the service as appropriate.
I, like others, put my code in the onHandleintent() as directed by the above documentation, however, ServiceTestCase only tests onStart and onStartCommand has shown above.
This is a bit late, but I just struggled with this. You could solve this by creating a class that simply overrides the onStart of you service so it calls
onHandleIntent
directly. So for instance, if you have a LocationUpdaterService, you could create a fake class that overrides theonStart
function like this:LocationUpdaterService
is a subclass of IntentService, so when you write your tests, just use theLocationUpdaterServiceFake
class like this}
Now whenever you call
startService
, it will bypass the threading code in IntentService and just call youronHandleIntent
functionThis is my approach for now:
The start Intent that invokes the service specifies the Service method to test
In the service onStart, we check for the specific Extra passed and call the method to test. This won't execute when Handle intent fired.
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 behindjunit
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 tostartService
returns. There is insufficient time foronHandleIntent
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.