简体   繁体   English

如何测试IntentService android?

[英]How to test IntentService android?

How can I test an IntentService in android without using the deprecated ServiceTestCase ? 如何在不使用不推荐使用的ServiceTestCase情况下在android中测试IntentService

From the documentation of the ServiceTestCase : ServiceTestCase的文档中:

This class was deprecated in API level 24. Use ServiceTestRule instead. 此类已在API级别24中弃用。请改用ServiceTestRule。 New tests should be written using the Android Testing Support Library. 新测试应使用Android测试支持库编写。

But the ServiceTestRule documentation says it doesn't support IntentService s: 但是ServiceTestRule文档说它不支持IntentService

Note: This rule doesn't support IntentService because it's automatically destroyed when onHandleIntent(android.content.Intent) finishes all outstanding commands. 注意:该规则不支持IntentService,因为当onHandleIntent(android.content.Intent)完成所有未完成的命令时,该规则会自动销毁。 So there is no guarantee to establish a successful connection in a timely manner. 因此,不能保证及时建立成功的连接。

How am I supposed to test an IntentService then ? 那我应该如何测试一个IntentService呢?

The service testing page in the android documentation says (emphasis mine): android文档中的服务测试页面说(重点是我):

Note: The ServiceTestRule class does not support testing of IntentService objects. 注意:ServiceTestRule类不支持对IntentService对象的测试。 If you need to test a IntentService object, you should encapsulate the logic in a separate class and create a corresponding unit test instead. 如果需要测试IntentService对象, 则应将逻辑封装在一个单独的类中,并创建一个相应的单元测试。

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. 但是,不推荐使用ServiceTestCase并不意味着您不能继续使用它-只是不鼓励这样做。 If using a deprecated interface is the only (or most efficient) way to accomplish your goal, then you should continue using it. 如果使用不推荐使用的接口是实现目标的唯一(或最有效的)方法,则应继续使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM