Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AndroidJUnit4.class is deprecated: How to use androidx.test.ext.junit.runners.AndroidJUnit4?

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?

like image 823
Marcel Gangwisch Avatar asked Oct 12 '18 09:10

Marcel Gangwisch


People also ask

Why do you use the Android JUnit runner when running UI tests?

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.

What is testInstrumentationRunner Androidx test runner AndroidJUnitRunner?

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.

Why do you use the AndroidJUnitRunner when running UI tests Mcq?

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.

What is activity test rule?

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.


2 Answers

According to the documentation for AndroidJUnit4,

  1. The gradle file should contain the following line:

androidTestImplementation 'androidx.test.ext:junit:1.1.1'

  1. Change test class to 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

like image 123
Marcel Gangwisch Avatar answered Sep 20 '22 07:09

Marcel Gangwisch


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.

like image 26
JJ Du Plessis Avatar answered Sep 19 '22 07:09

JJ Du Plessis