android - 如何在 AndroidUnitTest 中获取应用程序
问题描述
在 AndroidUnitTest 中,我需要将 Application 传递给其中一个类(实际上是我的网络管理器)。我怎样才能运行应用程序?
我试过了
instrumentationapplication = InstrumentationRegistry.getInstrumentation().targetContext as Application
但是,我得到了演员错误,它不起作用。
解决方案
你可以使用
val application = androidx.test.core.app.ApplicationProvider.getApplicationContext() as MyApplication
确保您有 androidx 测试库依赖项。
推荐阅读
- c# - SQL 如何替换一个值
- python - 使用 python 和请求,如何进行正确的 POST 调用以及在哪里找到标头?
- android - FirebaseAppDistribution:appDistributionUpload gradle 命令中缺少应用程序 ID
- mysql - 跨列和行计算值的实例
- javascript - 使用 redux 钩子时使用 redux 操作的最佳选择是什么?
- sql - Impala 2.11:AnalysisException:选择列表中不支持子查询
- swift - 在 SwiftUI 中获取当前的经纬度
- c++ - 是否可以禁止一个类被动态转换为?
- git - Git - 将发布分支合并到主分支
- tensorflow - 任何想法如何解决激活函数的问题?