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 IntentService
s:
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 ?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With