For my instrumentation tests I was using
@RunWith(AndroidJUnit4.class)
from
import androidx.test.runner.AndroidJUnit4;
in order to establish my test cases. Now this line gets marked as deprecated with the hint to use AndroidJUnit4
from
import androidx.test.ext.junit.runners.AndroidJUnit4
However if I try to import AndroidJUnit4
from the named package I get the error, that ext
can not be resolved.
Do you have an idea, what package should be included in gradle to resolve this issue?
When you use AndroidJUnitRunner to run your tests, you can access the context for the app under test by calling the static ApplicationProvider. getApplicationContext() method. If you've created a custom subclass of Application in your app, this method returns your custom subclass's context.
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" (from ToDoTests/build.gradle) This tells Android how to run our JUnit instrumented tests. JUnit uses “runner” classes for this role, and androidx. test.
Q15. Why do you use the AndroidJUnitRunner when running UI tests? The test runner facilitates loading your test package and the app under test onto a device or emulator, runs the test, and reports the results. The test runner creating screenshots of each screen that displayed while tests are executed.
public ActivityTestRule (Class<T> activityClass, boolean initialTouchMode) Similar to ActivityTestRule but defaults to launch the activity under test once per Test method. It is launched before the first Before method, and terminated after the last After method.
According to the documentation for AndroidJUnit4,
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
AndroidJUnit4ClassRunner
from AndroidJUnit4
If it still doesn't work, make sure that you clean and/or rebuild your project. Also you can check the current version directly in Google's maven repository
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.
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