When I updated my project to androidx then I've got following error
I also searched but can't find my answer, although project works fine
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
@Test
public void useAppContext() {
// Context of the app under test.
Context appContext = InstrumentationRegistry.getTargetContext();
assertEquals("com.example.villagefoodsecrets", appContext.getPackageName());
}
}
At this point, @RunWith(AndroidJUnit4.class)
shows that it is deprecated.
And Context appContext = InstrumentationRegistry.getTargetContext();
.getTargetContext; is in red format looks like not existed. So what can I do here - should I just ignore this?
To use non-deprecated classes, add below in build.gradle (app module level)
androidTestImplementation ‘androidx.test.ext:junit:1.1.1’
Then replace below deprecated imports in ExampleInstrumentedTest class
import androidx.test.InstrumentationRegistry;
import androidx.test.runner.AndroidJUnit4;
with
import androidx.test.platform.app.InstrumentationRegistry ;
import androidx.test.ext.junit.runners.AndroidJUnit4;
and to get a context use below:
for Java
Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
For Kotlin
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
wish this helps you
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With