0

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.

Tatsuya Fujisaki
  • 1,434
  • 15
  • 18

1 Answers1

0

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

Neethu
  • 1
  • 1