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