329

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?

Marcel Gangwisch
  • 8,856
  • 4
  • 23
  • 32
  • https://stackoverflow.com/questions/43342831/cannot-resolve-symbol-androidjunit4/43342884 – IntelliJ Amiya Oct 12 '18 at 09:44
  • 4
    I think your source is obsolet, they do not mention androidx at all. – Marcel Gangwisch Oct 12 '18 at 09:48
  • 2
    Unit testing on Android is an utter mess of bugs and deprecations. The only thing that works for me is creating a new scratch project, copying the relevant files from that project to mine, switching to debug mode, and doing a full cache invalidation. Also for AndroidJUnit4 tests the package name has to match the library package name. – Rupert Rawnsley Oct 04 '19 at 14:08

8 Answers8

475

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

Saikat
  • 14,222
  • 20
  • 104
  • 125
Marcel Gangwisch
  • 8,856
  • 4
  • 23
  • 32
  • 30
    my gradle has this and still cannot resolve the issue. – zuko Dec 01 '18 at 18:10
  • @zuko Clean and/or rebuild your project, it should be there. If still not and you're using modules, check that your dependency hierarchy is setup as you expect; you might need to add these to the top level modules too—since testing libraries aren't compiled into shared modules for use by upstream modules. – Jon Adams Dec 28 '18 at 01:58
  • @JonAdams my dependency tree kept holding onto a reference to older libraries for some reason even though i thought i removed them. so after trying various things i ended up just migrating the project to a new one and everything is fine now. – zuko Dec 29 '18 at 20:50
  • 8
    Should we also change _testInstrumentationRunner_ in the gradle file from _androidx.test.runner.AndroidJUnitRunner_ to _androidx.test.ext.junit.runners.AndroidJUnit4_? Just wondering if there's a mismatch. – Just The Highlights Feb 02 '19 at 09:17
  • 25
    @A Droid Replace old AndroidJUnit4 package in import with new one. Just Replace import androidx.test.runner.AndroidJUnit4; with import androidx.test.ext.junit.runners.AndroidJUnit4; – Xplosive Feb 08 '19 at 19:15
  • the new package name for AndroidJUnit4 is import androidx.test.ext.junit.runners.AndroidJUnit4. If you have your build.app proper , then it has to be an incorrect package name. Note that android studio wont do this on its own. Atleast not the version I am using Android Studio 3.3 updated on Dec 25,2018 – Muhammad Ahmed AbuTalib Mar 26 '19 at 10:22
  • @marcel-gangwisch Where in the linked document does it say to use that dependency? – Juan C Nuno Jul 17 '19 at 20:33
  • @Xplosive you should edit the answer to include your comment – Davide Aug 28 '19 at 08:23
  • Had this issue twice when creating new projects and adding 'androidx.test.ext:junit:1.1.1' solved it, just remember to remove the old imports from your 'androidTest ' directory and resync project – Jamal S Nov 29 '19 at 09:26
  • It is also worth mentioning that androidTest only builds [for a specific variant](https://developer.android.com/studio/build/gradle-tips#change-the-test-build-type) and that defaults to _debug_. This means it fails to resolve `AndroidJUnit4` when in release mode [unless you switch](https://stackoverflow.com/a/48843734/671393) the target variant. This is mercilessly opaque behaviour. – Rupert Rawnsley Jun 04 '20 at 10:47
  • As @RupertRawnsley mentioned, using `androidTestImplementation` adds the specified library to the class path for the Debug build variant when running Instrumentation (UI) Tests only. If you are using [`Roboelectric`](http://robolectric.org/) for example you may be attempting to use `@RunWith(AndroidJUnit4::class)` from local Unit Tests that run using the JVM and do not require the Emulator to be launched. More info [here](https://developer.android.com/studio/test/) – Jadent Jul 14 '21 at 09:03
50

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.

JJ Du Plessis
  • 1,047
  • 12
  • 9
25

For me the following steps worked:
1. Replace the androidx libraries with the one posted here . 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.

ansh sachdeva
  • 1,220
  • 1
  • 15
  • 32
  • 4
    `@RunWith(AndroidJUnit4ClassRunner.class)` was the part which got the issue resolved in my case. I don't really get why android studio generates deprecated code sometimes. – Rafsanjani Jun 18 '19 at 10:08
  • 1
    Use `ApllicationProvider.getApplicationContext()` instead of `InstrumentationRegistry.getInstrumentation().getTargetContext();` – Mitch Sep 20 '19 at 16:37
21

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

Saswata
  • 1,290
  • 2
  • 14
  • 28
HeshanHH
  • 653
  • 7
  • 9
10

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

Prox
  • 639
  • 10
  • 15
7
  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()

hata
  • 11,633
  • 6
  • 46
  • 69
Balsamiq
  • 71
  • 1
  • 2
6

In my case changing androidTestImplementation to testImplementation helped. I did't know the difference before reading this android difference between testImplementation and androidTestImplementation in build.gradle

3

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 !

Jeff T.
  • 2,193
  • 27
  • 32