Android 在插入指令的单元测试中,访问上下文的更好方法是什么?
在插入指令的单元测试中,使用Android 在插入指令的单元测试中,访问上下文的更好方法是什么?,android,androidx,android-context,ui-testing,instrumented-test,Android,Androidx,Android Context,Ui Testing,Instrumented Test,在插入指令的单元测试中,使用ApplicationProvider或InstrumentationRegistry访问Context时,以下哪项更为推荐 val-context:context=ApplicationProvider.getApplicationContext() 导入androidx.test.platform.app.InstrumentationRegistry val context:context=InstrumentationRegistry.getInstrume
ApplicationProvider
或InstrumentationRegistry
访问Context
时,以下哪项更为推荐
val-context:context=ApplicationProvider.getApplicationContext()
导入androidx.test.platform.app.InstrumentationRegistry
val context:context=InstrumentationRegistry.getInstrumentation().targetContext
我知道
androidx.test.InstrumentationRegistry
已被弃用,但androidx.test.platform.app.InstrumentationRegistry
未被弃用。您可以根据SDK版本使用androidx.test.platform.app.InstrumentationRegistry或android.support.test.InstrumentationRegistry,并通过
上下文上下文=InstrumentationRegistry.getInstrumentation().getTargetContext()