【问题标题】:How to test IntentService android?如何测试 IntentService android?
【发布时间】:2016-12-07 01:47:34
【问题描述】:

如何在不使用已弃用的 ServiceTestCase 的情况下在 android 中测试 IntentService

来自ServiceTestCase的文档:

此类在 API 级别 24 中已弃用。 请改用 ServiceTestRule。应使用 Android 测试支持库编写新测试。

但是ServiceTestRule 文档说它不支持IntentServices:

注意:此规则不支持 IntentService,因为它会在 onHandleIntent(android.content.Intent) 完成所有未完成的命令时自动销毁。 所以不能保证及时建立成功的连接。

那么我应该如何测试IntentService

【问题讨论】:

    标签: android unit-testing android-testing android-espresso intentservice


    【解决方案1】:

    android 文档中的service testing page 说(强调我的):

    注意:ServiceTestRule 类不支持测试 IntentService 对象。如果您需要测试 IntentService 对象,您 应该将逻辑封装在一个单独的类中并创建一个 相应的单元测试。

    所以官方的回答似乎是“改用单元测试”。然而,ServiceTestCase 被弃用并不意味着你不能继续使用它——只是不鼓励这样做。如果使用已弃用的接口是实现目标的唯一(或最有效)方法,那么您应该继续使用它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多