Android Unit Tests Requiring Context

You can use InstrumentationRegistry methods to get a Context:

InstrumentationRegistry.getTargetContext() – provides the application Context of the target application.

InstrumentationRegistry.getContext() – provides the Context of this Instrumentation’s package.


For AndroidX use InstrumentationRegistry.getInstrumentation().getTargetContext() or InstrumentationRegistry.getInstrumentation().getContext().

New API for AndroidX:
ApplicationProvider.getApplicationContext()

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)