Cannot resolve symbol 'AndroidJUnit4'

夙愿已清 提交于 2019-11-28 16:16:13
orium

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

mikes

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

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)

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.

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.

Andrew Glukhoff

In my case this helped for release variant:

android {
    ...
    testBuildType "release" 
}
tm81

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'

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'
})

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.

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

leonidaa

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 ?!

Adding

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

resolved this exact issue for me.

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.

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!