How to test IntentService android?

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

From the ServiceTestCase documentation:

This class is deprecated at API level 24. Instead, use a ServiceTestRule. New tests should be written using the Android Testing Support Library.

However, the ServiceTestRule documentation states that it does not support IntentService s:

Note. This rule does not support IntentService, because it is automatically destroyed when onHandleIntent (android.content.Intent) completes all outstanding commands. Thus, there is no guarantee of the timely establishment of a successful connection.

How can I check the IntentService then?

+5
source share
1 answer

the service testing page in the Android documentation is indicated (highlighted by me):

Note. The ServiceTestRule class does not support testing IntentService Objects. If you need to test the IntentService object , you must encapsulate the logic in a separate class and create the corresponding unit test.

So the official answer seems to be "use unit test". However, outdated ServiceTestCase does not mean that you cannot continue to use it - only this is not recommended. If using an outdated interface is the only (or most effective) way to achieve your goal, you should continue to use it.

+4
source

All Articles