Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/228.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 在插入指令的单元测试中,访问上下文的更好方法是什么?_Android_Androidx_Android Context_Ui Testing_Instrumented Test - Fatal编程技术网

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()