For my instrumentation tests I was using
@RunWith(AndroidJUnit4.class)
from
import androidx.test.runner.AndroidJUnit4;
Include these lines in build.gradle app module.
testImplementation 'androidx.test.ext:junit:1.1.1' androidTestImplementation 'androidx.test.ext:junit:1.1.1'
In the test class replace your test runner to
import androidx.test.ext.junit.runners.AndroidJUnit4;
If InstrumentationRegistry.getTargetContext()
is deprecated use InstrumentationRegistry
from androidx.test.platform.app
like this
InstrumentationRegistry.getInstrumentation().getTargetContext()