Cannot resolve symbol 'AndroidJUnit4'

前端 未结 19 2214
别跟我提以往
别跟我提以往 2020-12-02 08:48

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

import android.support.test.runn         


        
相关标签:
19条回答
  • 2020-12-02 09:33

    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.

    0 讨论(0)
  • 2020-12-02 09:34

    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.

    0 讨论(0)
  • 2020-12-02 09:35

    In my case this helped for release variant:

    android {
        ...
        testBuildType "release" 
    }
    
    0 讨论(0)
  • 2020-12-02 09:35

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

    0 讨论(0)
  • 2020-12-02 09:35

    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

    0 讨论(0)
  • 2020-12-02 09:37

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

    0 讨论(0)
提交回复
热议问题