0

更推荐Context在仪器化单元测试中访问以下哪项:使用ApplicationProviderInstrumentationRegistry

val context: Context = ApplicationProvider.getApplicationContext()
import androidx.test.platform.app.InstrumentationRegistry

val context: Context = InstrumentationRegistry.getInstrumentation().targetContext

我知道androidx.test.InstrumentationRegistry已弃用,但androidx.test.platform.app.InstrumentationRegistry不是。

4

1 回答 1

0

您可以根据您的 SDK 版本使用 androidx.test.platform.app.InstrumentationRegistry 或 android.support.test.InstrumentationRegistry 并通过以下方式获取上下文

上下文上下文 = InstrumentationRegistry.getInstrumentation().getTargetContext();

于 2020-09-08T12:34:17.453 回答