158

Obviously I need the correct import statment to solve this problem. According to the docs for AndroidJUnit4, this should be

import android.support.test.runner.AndroidJUnit4;

When I do that, Android Studio highlights runner in red and complains "Cannot resolve symbol 'runner'".

Background

I got to this point by following the tutorials on the Android Developer site for setting up tests using UI Automator. The first problem I encountered was that com.android.support:support-v4:22.2.0 and com.android.support.test:runner:0.2 depend on different versions of com.android.support:support-annotations. I followed the suggestions from this Android bug report and added the following to allprojects in my project's build.gradle:

configurations.all {
    resolutionStrategy.force 'com.android.support:support-annotations:22.1.0'
}

This solved the immediate error, but I suspect it lead to my current problems. Does anyone have any suggestions about how to fix this?

Relevent sections from `./gradlew :app:dependencies

androidTestCompile - Classpath for compiling the androidTest sources.
+--- com.jayway.android.robotium:robotium-solo:5.2.1
+--- com.squareup:fest-android:1.0.8
|    \--- org.easytesting:fest-assert-core:2.0M10
|         \--- org.easytesting:fest-util:1.2.5
+--- com.android.support.test:runner:0.2
|    +--- junit:junit-dep:4.10
|    |    \--- org.hamcrest:hamcrest-core:1.1
|    +--- com.android.support.test:exposed-instrumentation-api-publish:0.2
|    \--- com.android.support:support-annotations:22.0.0 -> 22.2.0
+--- com.android.support.test:rules:0.2
|    \--- com.android.support.test:runner:0.2 (*)
\--- com.android.support.test.uiautomator:uiautomator-v18:2.1.0

compile - Classpath for compiling the main sources.
+--- com.android.support:appcompat-v7:22.2.0
|    \--- com.android.support:support-v4:22.2.0
|         \--- com.android.support:support-annotations:22.2.0
+--- com.android.support:support-v4:22.2.0 (*)
+--- com.google.android.gms:play-services:6.1.71
|    \--- com.android.support:support-v4:20.0.0 -> 22.2.0 (*)
+--- com.crashlytics.android:crashlytics:1.+ -> 1.1.13
\--- com.jakewharton:butterknife:5.1.2
Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
  • That suggests that your build is missing `com.android.support.test:runner` outright. Can you confirm via `gradle dependencies` that you're still pulling this in after the `resolutionStrategy` change? – CommonsWare Jun 02 '15 at 18:16
  • @CommonsWare Added dependencies output to my question. I don't know what the `(*)` means after `com.android.support.test:runner:0.2`. – Code-Apprentice Jun 02 '15 at 18:23
  • According to [the Gradle folks](https://discuss.gradle.org/t/what-is-the-asterisk-in-dependencyreport/6067/3), the asterisk "means that the tree view of the dependency graph is cut short at this point because that part of the graph was already listed earlier." I'm stumped as to why this isn't working for you. – CommonsWare Jun 02 '15 at 18:26
  • 1
    Note that @stemadsen 's answer from 2018 is potentially more relevant than the others. Someone once wrote about a test that kept the questions the same year after year, but the answers changed... – Roy Falk Jun 11 '18 at 07:17

19 Answers19

209

Make sure your app in debug build variant. Go to Build > Select Build Variant... and the following should show up:

enter image description here

Willi Mentzel
  • 27,862
  • 20
  • 113
  • 121
orium
  • 3,743
  • 2
  • 24
  • 27
  • 5
    Thanks, you saved me – Sofa May 27 '16 at 13:10
  • 11
    Bless you. The documentation never states "this stuff will only work in debug". So frustrating. – Chantell Osejo Jun 23 '16 at 23:01
  • 28
    Wow. Up-Vote x 1million. This is the answer I have been searching for for days. Also found this so you can change which build type to use for it. `android { testBuildType "staging"} ` – WIllJBD Jun 30 '16 at 17:28
  • 6
    I am going to have to file a bug report for this with the Android Studio Tools team. Even build types that inherit from `debug` don't work unless explicitly set to "debug" – Gautham C. Jul 18 '16 at 15:25
  • Thank you, this was my problem. Note that, to add to the confusion, my tests will still execute properly from the command line even if I've configured Android Studio to generate a release build. – stevehs17 Oct 29 '16 at 22:53
  • 2
    wow, I am using a cutsom buildType with initWith(buildTypes.debug) and still fails. Only and only if I am using the debug directly works! – narancs Jul 27 '17 at 21:53
  • In fact, it seems that the `testBuildType` you specify and the current Build Variant of the project needs to match. I had `testBuildType "release"` set and was in debug variant. When I switched to `release` variant, everything worked fine. – alexbchr Jul 12 '18 at 13:53
  • Can anyone explain, why is this happening ? – Ezio Dec 12 '18 at 09:15
  • It worked for me it is the most common thing I thing – Michał Ziobro Jan 08 '19 at 09:05
  • This saved my mental energy and time thinking why my test classes has lots of errors. Thanks a lot! – Glenn Jul 12 '19 at 07:52
137

I made the mistake to put the test classes at src/test. After moving them to src/androidTest/java/ the dependency was resolved.

Willi Mentzel
  • 27,862
  • 20
  • 113
  • 121
mikes
  • 2,323
  • 1
  • 16
  • 11
78

Ok so here is your mistake and mine!

If we are going to write a pice of code for Local Unit Testing we shouldn't use @RunWith(AndroidJUnit4.class) cause we do not use AndroidJUnit4 but we need Junit4. so we should write @RunWith(JUnit4.class). And of course your java test file is under app/src/test/java/your.package.name directory.

Else if (!!) we want to write some Android Instrumented Unit Test we should put our test java files in app/src/androidTest/java/your.package.name directory and use annotation like @RunWith(AndroidJUnit4.class)

Sepehr Behroozi
  • 1,780
  • 1
  • 17
  • 32
38

Update

The Android Test Library is now part of AndroidX. Be sure to use the correct Gradle dependencies found in the official documentation.

Original Answer

I found here that there are newer versions of the Testing Support Library than what I was using:

dependencies {
    androidTestCompile 'com.android.support.test:runner:0.5'
    androidTestCompile 'com.android.support.test:rules:0.5'
    androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.2'
}

Note: Be sure to use the most recent versions of these libraries. This question is from a time when the Android Test Support Library was new and the version numbers here are very out of date.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
  • 3
    I wonder why the [documentation](https://developer.android.com/training/testing/integration-testing/service-testing#java) mentions **nothing** about this, unless I missed it. Would have saved me some grief. – Nom1fan Jul 27 '18 at 11:04
  • @Karoly I still have problem. Did you find any solution? – Mahdi Jan 13 '19 at 15:38
  • The official documentation linked in the answer doesn't list the needed gradle dependencies. Fortunately, the Migrate to AndroidX tool is able to convert the dependencies in the answer to: `dependencies { androidTestImplementation 'androidx.test.ext:junit:1.1.1' androidTestImplementation 'androidx.test:rules:1.1.1' androidTestImplementation 'androidx.test.uiautomator:uiautomator:2.2.0' }` – user2623008 Sep 20 '20 at 17:50
  • 1
    @user2623008 This answer was written well before the creation of the `androidx` namespace. Thanks for the update. – Code-Apprentice Sep 21 '20 at 15:15
26

I solved the problem by making a small change in the app's build.gradle file. In the dependencies { ... } section, make sure to include the following line:

debugImplementation 'com.android.support.test:runner:1.0.1'

or whatever version is the newest at that point (...Compile is deprecated and has been replaced by ...Implementation). Note the use of debugImplementation. Android Studio suggested auto-including it with androidTestImplementation, which didn't work.

I found out how to change it from test to debug by looking in Project Structure under Dependencies of the app module, where you can change the scope of each dependency, see below.

Project structure

stemadsen
  • 1,841
  • 2
  • 16
  • 15
20

The common cause of this problem is that when adding below dependency:

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

This is a correct dependency if you are going to use instrumented tests (tests in the androidTest java package)

But for implementing local unit tests (tests in test java package) while using above mentioned dependency; then you'll face Cannot resolve symbol 'AndroidJUnit4'

That is because androidTestImplementation directive is used to import libraries in instrumented tests, but not in the local JVM/unit tests.

If you want to use AndroidJUnit4 in a local JVM/unit test, then use below dependency instead

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

The same thing applies if you add the latter dependency while using AndroidJUnit4 in instrumented test, you will also get Cannot resolve symbol 'AndroidJUnit4'; because you're using the wrong directive.

Zain
  • 37,492
  • 7
  • 60
  • 84
  • 1
    In the scenario in my original question, I was running instrumented tests. However, this is a great distinction to make and hopefully will help visitors to this question in the future. – Code-Apprentice Mar 03 '20 at 20:20
12

Notice that this the OP is now in 2019 , 4 years old so If you are using Android X then AndroidJUnit4.class is deprecated , you have an error there and one more with this androidx.test.ext.junit.runners.AndroidJUnit4. I suggest to read this links to solve the problem .

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

Migrating Junit4 tests to androidx: What causes 'delegate runner could not be loaded'? For me Android Studio suggested to replace

@RunWith(AndroidJUnit4.class)

which was deprecated with

@RunWith(AndroidJUnit4ClassRunner.class)

and this

androidx.test.ext.junit.runners.AndroidJUnit4

with this

import androidx.test.internal.runner.junit4.AndroidJUnit4ClassRunner;

After that the error is gone but I don't know if the future test while run ok ?!

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
leonidaa
  • 392
  • 1
  • 7
  • 12
8

In my case this helped for release variant:

android {
    ...
    testBuildType "release" 
}
Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
Andrew Glukhoff
  • 898
  • 11
  • 9
  • Can you give a more complete solution. I don't understand the context of what you did. – Code-Apprentice Feb 17 '18 at 17:03
  • I encountered "Cannot resolve symbol 'AndroidJUnit4'" issue on my espresso test when I changed build variant to "release". As soon as I added this statement (testBuildType "release") to app-level build.gradle (taken from https://developer.android.com/studio/test/index.html#add_a_new_test) this issue disappeared. – Andrew Glukhoff Feb 17 '18 at 20:12
  • I formatted your answer the code in your answer. You should add more details, such as the link from your comment and describe where this block goes in your project. – Code-Apprentice Feb 17 '18 at 20:46
7

If anyone still having this issue:

Cannot resolve symbol 'AndroidJUnit4'

and using API 27, in the build.gradle which is in the app module, add the following lines:

testImplementation 'junit:junit:4.12'

// AndroidJUnitRunner and JUnit Rules
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test:rules:1.0.2'

// Espresso dependencies
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
Randika Vishman
  • 7,983
  • 3
  • 57
  • 80
tm81
  • 328
  • 3
  • 6
3

put this code in your Dependencies

compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
    exclude group: 'com.android.support', module: 'support-annotations'
})
Paramatma Sharan
  • 437
  • 5
  • 11
  • 1
    When I wrote this question, I had not yet added espresso as a dependency for unit tests. – Code-Apprentice Jan 04 '17 at 13:08
  • +1 I actually figured it out right before I saw this, but yes, this is what I did and it solved my problem. – Tony D Jan 13 '17 at 22:11
  • 2
    Follow the two answers below from @sepehr . AndroidJunit4 class is not in espresso package insted its present in android.support.test.runner package. and all you have to do is include @Runwith(AndroidJunit4.class) is write in above/for test case present in src/Androidtest/java , INSTED OF including in test cases written under src/test/java. – Khay Jul 21 '17 at 19:03
3

Move the test class to src/androidTest/java/. Then dependency will resolve.

Komal Nikhare
  • 252
  • 1
  • 4
  • 9
2

If you're using project with multiple build-type then the selected build-type in build-variant window must be mentioned with testBuildType tag in module's build.gradle file.

For e.g.: If you're using build type debug then you should add android{testBuildType "debug" }, if using stage then add android{testBuildType "stage"} statement in android tag.

Rahul Rastogi
  • 4,486
  • 5
  • 32
  • 51
  • Are you suggesting to edit the build file if you want to switch the variant being tested? – nasch Oct 24 '19 at 15:21
2

Add this dependency in your build.gradle file:

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

Update the end version (1.1.1) with the latest version released.

RobC
  • 22,977
  • 20
  • 73
  • 80
brewmeakay
  • 21
  • 2
1

The classical Invalidate Caches/Restart has helped me! :)

user_MGU
  • 978
  • 11
  • 11
1

The same error occurred to me when I follow Google IOSched app and set up my project with three build types [debug,release,staging] where debug and release share the same source directory

sourceSets {
    debug.java.srcDir 'src/debugRelease/java'
    release.java.srcDir 'src/debugRelease/java'
}

In this case, specify the testBuildType in your module-level build.gradle file and the project should now be able to resolve symbol 'AndroidJUnit4'.

...
sourceSets {
    debug.java.srcDir 'src/debugRelease/java'
    release.java.srcDir 'src/debugRelease/java'
}

testBuildType "staging"
...

Reference: https://github.com/google/iosched/blob/master/mobile/build.gradle

Jie Heng
  • 196
  • 4
0

Adding

compile com.android.support.test:runner:0.5'

resolved this exact issue for me.

donlys
  • 440
  • 6
  • 13
  • This is already stated in the accepted answer. You should also see if there is a more recent version and use that instead. – Code-Apprentice Mar 31 '18 at 16:40
  • Is androidTestCompile same as compile? – donlys Apr 01 '18 at 20:04
  • `compile` directives name dependencies used in the main app while `androidTestComiple` names dependencies used for testing. So the correct answer is to use `androidTestCompile`, not `compile`. – Code-Apprentice Apr 01 '18 at 20:09
0

As the list of answers demonstrate, this can be caused by a few things. One more for the list:

I ran an over-zealous LINT which removed all unused imports. This will produce the same errors, and it is easy to miss that this is the problem.

Android-studio will highlight references that are missing in the test code - and the ALT-ENTER popup will appear (this is the bit that is easy to miss).

Next, I need to remove the tests from LINT - or at least disable this warning.

Edit: @Code-Apprentice, the lines that were missing were:

import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;


import static junit.framework.Assert.assertNotNull;
import static junit.framework.Assert.assertNull;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

So the first error in the file was with @RunWith(AndroidJUnit4.class) at the beginning of my test class.

winwaed
  • 7,645
  • 6
  • 36
  • 81
0

Short Story Version:

Upgrade your Gradle to the latest Version

I am answering this question post on Feb, 15th, 2020. Unfortunately, I have exhausted every possible solutions mentioned here and elsewhere.

Yes, none of them above workes. I use the built-in function, "Migrate to Andoridx", it may remind me that I have to upgrade my target SDK versions and my gradle version. After I upgraded my gradle version from 2.0.2 to 3.5.3. They just works, even the old import statement works.

  • Hi, this is a old question already has an accepted answer and i fail to see how your answer adds anything new to the question. Please avoid answering if your answer dosen't add anything new to the existing answers. Thanks –  Feb 15 '20 at 09:25
  • The accepted answer gives a link to update the testing dependencies to the AndroidX Testing library. This is likely the thing that fixed it in all the things you tried. – Code-Apprentice Feb 15 '20 at 16:08
0

Make sure you have

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

dependency in your app build.gradle file. I deleted it by mistake and test stopped wroking.

Shubham Goel
  • 1,962
  • 17
  • 25