Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot resolve symbol 'AndroidJUnit4'

People also ask

What is AndroidJUnit4?

AndroidJUnit4 is the class test runner. This is the thing that will drive the tests for a single class. You annotate your test classes with this: @RunWith(AndroidJUnit4. class) public class MyActivityTest { ... }

Why do you use the AndroidJUnitRunner 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.


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

enter image description here


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.

Project structure