首页 > 解决方案 > 如何在 AndroidUnitTest 中获取应用程序

问题描述

在 AndroidUnitTest 中,我需要将 Application 传递给其中一个类(实际上是我的网络管理器)。我怎样才能运行应用程序?

我试过了

instrumentationapplication = InstrumentationRegistry.getInstrumentation().targetContext as Application

但是,我得到了演员错误,它不起作用。

标签: androidunit-testingkotlinandroidx

解决方案


你可以使用

val application = androidx.test.core.app.ApplicationProvider.getApplicationContext() as MyApplication

确保您有 androidx 测试库依赖项。


推荐阅读