I was able to successfully test my IntentService , and I will show you conceptually how I did it.
First, you extend the Android service testing class: ServiceTestCase<MyIntentService> . Then you basically start the IntentService , as you did, using startService(intent) .
Since the service under test is an IntentService , it will run in the IntentService . If you do not block the test thread, then your test method will immediately make statements that will obviously fail, because the test work in the background is probably not completed yet. In the end, the test method will return and your test will fail.
What you need to do is block the test thread after startService . Do this using a ReentrantLock and a condition that calls await() on it.
Then the IntentService runs onHandleIntent in the background. I suggest that you expand your IntentService and override onHandleIntent by calling super.onHandleIntent() , and then tell the test thread that the work is done. Do this with the same lock and condition used to lock the test thread.
source share