For my instrumentation tests I was using
@RunWith(AndroidJUnit4.class)
from
import androidx.test.runner.AndroidJUnit4;
If you've tried @MarcelGangwisch's solution and your build fails saying it can't find the resource AND you also cleaned/rebuilt your project and it still doesn't work, try this: (based also on @KrzysztofDziuba's solution)
In your gradle file where you changed the dependency, make sure you are adding it as the type you need, ie.:
For UI tests:
androidTestImplementation 'androidx.test.ext:junit:1.1.0'
For Unit tests:
testImplementation 'androidx.test.ext:junit:1.1.0'
In my instance I added it as both and now it works.
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()