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

AndroidJunitAndroid TestingAndroidx

Android Problem Overview


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?

Android Solutions


Solution 1 - Android

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

Solution 2 - Android

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.

Solution 3 - Android

For me the following steps worked:

  1. Replace the androidx libraries with the one posted [here][1] . my final app/build.gradle looked like this:

    android { ... defaultConfig { ... testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

     }
     ...
    

    }

    dependencies { ... testImplementation 'junit:junit:4.12'

     // Core library
     androidTestImplementation 'androidx.test:core:1.2.0'
    
     // AndroidJUnitRunner and JUnit Rules
     androidTestImplementation 'androidx.test:runner:1.2.0'
     androidTestImplementation 'androidx.test:rules:1.2.0'
    
     // Assertions
     androidTestImplementation 'androidx.test.ext:junit:1.1.1'
     androidTestImplementation 'androidx.test.ext:truth:1.2.0'
     androidTestImplementation 'com.google.truth:truth:0.42'
    
     // Espresso dependencies
     androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
     
    

    }

I then manually replaced the imported modules in my ExampleInstrumentTest.java class with latest classes:

import androidx.test.internal.runner.junit4.AndroidJUnit4ClassRunner;
import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.rule.ActivityTestRule;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

@RunWith(AndroidJUnit4ClassRunner.class)
public class ExampleInstrumentedTest {
    ...
    @Rule
    public final ActivityTestRule<MainActivity> main = new ActivityTestRule<>(MainActivity.class, true);

    @Before
    public void init() {
        ...
    }
    @Test
    public void listCount() {
        ...
    }

    @Test
    public void useAppContext() {
        Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();

        Assert.assertEquals("in.curioustools.aad_x_testing2", appContext.getPackageName());
        System.out.println("useAppContext : Test Ran");
    }
}

The thing that was bugging me was the fact that InstrumentationRegistery class was still deprecated. So i used InstrumentationRegistry.getInstrumentation().getTargetContext(); from androidx.test.platform.app.InstrumentationRegistry class. [1]: https://developer.android.com/training/testing/set-up-project#gradle-dependencies

Solution 4 - Android

Solution

  1. Add this line in to build.gradle: androidTestImplementation 'androidx.test.ext:junit:1.1.1'

  2. Change AndroidJUnit4 to AndroidJUnit4ClassRunner in the test class

Warning

I got the same error, but the following solution failed for me:

File -> Invalidate Caches..., and select Invalidate and Restart

Solution 5 - Android

I tried all given above until I went to the official Android site, and they suggested importing from androidx.test.ext.junit.runners.AndroidJUnit4 instead of androidx.test.runner.AndroidJUnit4. link

Solution 6 - Android

  1. Include these lines in build.gradle app module.

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

  2. In the test class replace your test runner to

    import androidx.test.ext.junit.runners.AndroidJUnit4;

  3. If InstrumentationRegistry.getTargetContext() is deprecated use InstrumentationRegistry from androidx.test.platform.app like this

    InstrumentationRegistry.getInstrumentation().getTargetContext()

Solution 7 - Android

In my case changing androidTestImplementation to testImplementation helped. I did't know the difference before reading this https://stackoverflow.com/questions/52076775/android-difference-between-testimplementation-and-androidtestimplementation-in-b

Solution 8 - Android

If you imported those AnroidX test libraries, synced and re-built the project, but the imported packages were still not resolved. Just remember how you upgraded your project to AndroidX, close Android Studio and remove the .idea folder and reopen your project again...

This worked for me !

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMarcel GangwischView Question on Stackoverflow
Solution 1 - AndroidMarcel GangwischView Answer on Stackoverflow
Solution 2 - AndroidJJ Du PlessisView Answer on Stackoverflow
Solution 3 - Androidansh sachdevaView Answer on Stackoverflow
Solution 4 - AndroidHeshanHHView Answer on Stackoverflow
Solution 5 - AndroidProxView Answer on Stackoverflow
Solution 6 - AndroidBalsamiqView Answer on Stackoverflow
Solution 7 - AndroidKrzysztof DziubaView Answer on Stackoverflow
Solution 8 - AndroidJeff T.View Answer on Stackoverflow