Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test IntentService android?

How can I test an IntentService in android without using the deprecated ServiceTestCase?

From the documentation of the ServiceTestCase:

This class was deprecated in API level 24. Use ServiceTestRule instead. New tests should be written using the Android Testing Support Library.

But the ServiceTestRule documentation says it doesn't support IntentServices:

Note: This rule doesn't support IntentService because it's automatically destroyed when onHandleIntent(android.content.Intent) finishes all outstanding commands. So there is no guarantee to establish a successful connection in a timely manner.

How am I supposed to test an IntentService then ?

like image 433
Timothée Jeannin Avatar asked Aug 01 '16 20:08

Timothée Jeannin


1 Answers

The service testing page in the android documentation says (emphasis mine):

Note: The ServiceTestRule class does not support testing of IntentService objects. If you need to test a IntentService object, you should encapsulate the logic in a separate class and create a corresponding unit test instead.

So the official answer would seem to be "use a unit test instead." However, ServiceTestCase being deprecated doesn't mean you can't continue to use it -- just that doing so is discouraged. If using a deprecated interface is the only (or most efficient) way to accomplish your goal, then you should continue using it.

like image 137
alzee Avatar answered Sep 24 '22 21:09

alzee