Which of the following is more recommended to access Context
in instrumented unit tests: using ApplicationProvider
or InstrumentationRegistry
?
val context: Context = ApplicationProvider.getApplicationContext()
import androidx.test.platform.app.InstrumentationRegistry
val context: Context = InstrumentationRegistry.getInstrumentation().targetContext
I know androidx.test.InstrumentationRegistry
is deprecated, but androidx.test.platform.app.InstrumentationRegistry
is not.
You can either use androidx.test.platform.app.InstrumentationRegistry or android.support.test.InstrumentationRegistry based on your SDK version and get the context by
Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.