【问题标题】:Android Espresso : cannot resolve symbol AndroidJUnit4.classAndroid Espresso:无法解析符号 AndroidJUnit4.class
【发布时间】:2015-11-17 11:53:48
【问题描述】:

我正在尝试在新的 Android 项目中创建 Espresso UI 测试,但遇到了以下问题。

如果我尝试创建一个空的测试类:

import android.content.Intent;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;
import android.test.ActivityInstrumentationTestCase2;

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

import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withText;


@RunWith(AndroidJUnit4.class)
public class LoginActivityTest extends ActivityInstrumentationTestCase2<LoginActivity> {

}

我总是收到此错误消息:

cannot resolve symbol AndroidJUnit4.class

而且几乎所有导入的库都被标记​​为未使用。

build.gradle 文件包含以下内容:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.0"

    defaultConfig {
        applicationId "com.some.thing.xxx"
        minSdkVersion 14
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    lintOptions {
        abortOnError false
    }
    packagingOptions {
        exclude 'LICENSE.txt'
    }
}

repositories {
    mavenCentral()
    maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
    maven { url "https://jitpack.io" }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:23.0.0'
    compile 'com.google.android.gms:play-services:7.8.0'
    compile 'com.mcxiaoke.volley:library:1.0.18'
    compile 'com.orhanobut:logger:1.11'
    // App dependencies
    compile 'com.android.support:support-annotations:23.0.0'
    // TESTING DEPENDENCIES
    androidTestCompile 'com.android.support.test:runner:0.3'
    // Set this dependency to use JUnit 4 rules
    androidTestCompile 'com.android.support.test:rules:0.3'
    // Set this dependency to build and run Espresso tests
    androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2'
    // add this for intent mocking support
    androidTestCompile 'com.android.support.test.espresso:espresso-intents:2.2'
    // add this for webview testing support
    androidTestCompile 'com.android.support.test.espresso:espresso-web:2.2'
    // Set this dependency to build and run UI Automator tests
    androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.1'
    androidTestCompile 'com.android.support.test.espresso:espresso-contrib:2.2'
}

如果我将这些设置放在我的其他测试项目中,它可以工作,所以我不知道会出现什么问题?

我已按照本教程进行操作:"

http://www.vogella.com/tutorials/AndroidTestingEspresso/article.html

我已经尝试通过以下 SO 问题来解决它: Cannot resolve symbol 'AndroidJUnit4'

但没有运气。

非常感谢您的任何建议。

【问题讨论】:

  • android { testBuildType "debug" } 为我工作就像一个魅力。

标签: android unit-testing user-interface android-espresso


【解决方案1】:

我也尝试过 vogella 的相同教程,但遇到了很多问题。我遇到的第一个问题是 v23 库的注释版本和 Espresso 库之间的依赖冲突。

然后我找到了 Roger Hu 的另一个最近更新的教程“UI Testting with Espresso”。我注意到 Espresso 还不支持 Marshmallow 的评论。

添加的依赖如下:

androidTestCompile('com.android.support.test.espresso:espresso-core:2.2') {
    // Necessary if your app targets Marshmallow (since Espresso
    // hasn't moved to Marshmallow yet)
    exclude group: 'com.android.support', module: 'support-annotations'
}
androidTestCompile('com.android.support.test:runner:0.3') {
    // Necessary if your app targets Marshmallow (since the test runner
    // hasn't moved to Marshmallow yet)
    exclude group: 'com.android.support', module: 'support-annotations'
}

这解决了我的依赖冲突,我没有看到任何其他问题发生。

【讨论】:

  • 你是一个救生员。只是为了让其他人清楚,只需使用上面的依赖项。没有其他的。否则你仍然会遇到依赖冲突。
【解决方案2】:

我通过手动导入以下解决了它,我认为它应该自动导入但它没有:

import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.action.ViewActions.click;
import static android.support.test.espresso.action.ViewActions.closeSoftKeyboard;
import static android.support.test.espresso.action.ViewActions.typeText;
import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withText;

【讨论】:

    【解决方案3】:

    您收到该错误消息的原因可能是您的测试所在的文件夹与规范不匹配。该文件夹必须是 src/androidTest/java

    Take a look at this article 上面写着...

    运行仪表化单元测试要运行仪表化测试,请按照 这些步骤:

    通过单击“同步”确保您的项目与 Gradle 同步 工具栏中的项目。以下列方式之一运行您的测试: 要运行单个测试,请打开“项目”窗口,然后右键单击 测试并单击运行。要测试类中的所有方法,请右键单击 测试文件中的类或方法,然后单击 Run 。在一个 目录,右键单击该目录并选择 Run tests 。这 Android Plugin for Gradle 编译位于 在默认目录 (src/androidTest/java/) 中,构建一个测试 APK 和生产 APK,在连接的设备上安装这两个 APK 或 模拟器,并运行测试。 Android Studio 然后显示结果 在“运行”窗口中执行检测的测试。

    因此各位,instrumentation test 的文件夹必须是(不要忘记大小写)

    src/androidTest/java

    对于本地测试,文件夹必须是

    src/test/java

    然后你可以让你的包文件夹匹配你的应用包

    希望,这对社区有所帮助!

    【讨论】:

    • 在其他情况下也会出现此错误,例如测试/产品依赖项的版本冲突。
    • 文件夹一直是androidTest!谢谢!
    【解决方案4】:

    我通过改变常数解决了这个问题

    minSdkVersion 
    

    build.gradle 文件中的版本 18。

    以下 gradle.file 正在运行:

    apply plugin: 'com.android.application'
    
    android {
        compileSdkVersion 23
        buildToolsVersion "23.0.0"
    
        defaultConfig {
            applicationId "com.something.xxx"
            minSdkVersion 18
            targetSdkVersion 23
            versionCode 1
            versionName "1.0"
            testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        }
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
        }
        lintOptions {
            abortOnError false
        }
        packagingOptions {
            exclude 'LICENSE.txt'
        }
    }
    
    repositories {
        mavenCentral()
        maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
        maven { url "https://jitpack.io" }
    }
    
    dependencies {
        compile fileTree(dir: 'libs', include: ['*.jar'])
        compile 'com.android.support:appcompat-v7:23.0.0'
        compile 'com.google.android.gms:play-services:7.8.0'
        compile 'com.mcxiaoke.volley:library:1.0.18'
        compile 'com.orhanobut:logger:1.11'
    
        // TESTING DEPENDENCIES
        androidTestCompile 'com.android.support:support-annotations:23.0.0'
        androidTestCompile 'com.android.support.test:runner:0.3'
        // Set this dependency to use JUnit 4 rules
        androidTestCompile 'com.android.support.test:rules:0.3'
        // Set this dependency to build and run Espresso tests
        androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2'
        // add this for intent mocking support
        androidTestCompile 'com.android.support.test.espresso:espresso-intents:2.2'
        // add this for webview testing support
        androidTestCompile 'com.android.support.test.espresso:espresso-web:2.2'
        // Set this dependency to build and run UI Automator tests
        androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.1'
        androidTestCompile 'com.android.support.test.espresso:espresso-contrib:2.2'
    }
    

    【讨论】:

    • 更改sdk版本也为我解决了,但有趣的是它工作一次后,我将它设置回19,它仍然有效!
    • 这可以工作,但我不强烈推荐这个解决方案。例如,我正在苦苦挣扎的乞丐,使用这种解决方案,我将失去相当大的市场份额。 ://
    【解决方案5】:

    根据上面给出的 gradle 变化:

    androidTestCompile 'com.android.support.test:runner:0.3'
    

    你需要改成

    androidTestCompile('com.android.support.test:runner:0.3') {
        exclude group: 'com.android.support', module: 'support-annotations'
    }
    

    对我来说,即使进行了上述更改,它也无法正常工作,所以我注意到我缺少以下内容:

    androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.1'
    

    对我来说效果很好。

    完整的 build.gradle 如下:

        apply plugin: 'com.android.application'
    
        android {
        compileSdkVersion 23
        buildToolsVersion "21.1.2"
    
        lintOptions {
            // IMPORTANT: We are disabling this rule to avoid build errors on PrettyTime. Although
            //pretty time references an InvalidPackage it does not do it in the code sections we use
            //given how easy this library is to use I would prefer not to replace it with something
            //like Joda-Time which is overkill for such a small section of the app.
            disable 'InvalidPackage'
        }
    
        packagingOptions {
            exclude 'LICENSE.txt'
        }
    
        defaultConfig {
            applicationId "co.test.dialer"
            minSdkVersion 18
            targetSdkVersion 22
            versionCode 15
            versionName "0.6.15."
            renderscriptTargetApi 22
            renderscriptSupportModeEnabled true
            testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        }
    
        signingConfigs {
            production {
                storeFile file("keystore.jks")
                storePassword "hello"
                keyAlias "production"
                keyPassword "android"
            }
    
            debug {
                storeFile file("keystore.jks")
                storePassword "hello"
                keyAlias "debug"
                keyPassword "android"
            }
    
        }
    
        buildTypes {
    
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
                signingConfig signingConfigs.production
            }
    
            debug {
                minifyEnabled false
                debuggable true
                applicationIdSuffix ".debug"
                signingConfig signingConfigs.debug
            }
    
            internal_test {
                minifyEnabled false
                debuggable true
                applicationIdSuffix ".test"
                signingConfig signingConfigs.debug
            }
        }
    }
    
        dependencies {
        compile fileTree(dir: 'libs', include: ['*.jar'])
    
        compile 'com.android.support:appcompat-v7:23.0.1'
        compile 'com.android.support:support-v13:23.0.1'
        compile 'com.android.support:cardview-v7:23.0.1'
        compile 'com.android.support:design:23.0.1'
        compile 'com.android.support:recyclerview-v7:23.0.1'
        compile 'com.google.android.gms:play-services-gcm:8.1.0'
        compile 'com.jakewharton:butterknife:6.1.0'
        compile 'com.afollestad:material-dialogs:0.7.8.0'
        compile 'com.googlecode.libphonenumber:libphonenumber:3.1'
        compile 'com.mcxiaoke.volley:library:1.0.15'
        compile 'squizbit.com.jsonobjectified:jetjson:1.0.3@aar'
        compile 'com.google.android.gms:play-services-analytics:8.1.0'
    
        releaseCompile 'co.test.dialersdk:dialersdk:1.0.8@aar';
        debugCompile 'co.test.dialersdk:dialersdk-debug:1.0.8@aar';    
        internal_testCompile 'co.test.dialersdk:dialersdk-internal_test:1.0.8@aar';
    
        androidTestCompile('com.android.support.test:runner:0.3') {
            exclude group: 'com.android.support', module: 'support-annotations'
        }
        androidTestCompile('com.android.support.test:rules:0.3') {
            exclude group: 'com.android.support', module: 'support-annotations'
        }
        androidTestCompile('com.android.support.test.espresso:espresso-core:2.2') {
            exclude group: 'com.android.support', module: 'support-annotations'
        }
        androidTestCompile('com.android.support.test.espresso:espresso-intents:2.2') {
            exclude group: 'com.android.support', module: 'support-annotations'
        }
        androidTestCompile('com.android.support.test.espresso:espresso-contrib:2.2') {
            exclude group: 'com.android.support', module: 'support-annotations'
            exclude group: 'com.android.support', module: 'appcompat'
            exclude group: 'com.android.support', module: 'support-v4'
            exclude module: 'recyclerview-v7'
        }
        androidTestCompile('com.android.support.test.espresso:espresso-web:2.2') {
            exclude group: 'com.android.support', module: 'support-annotations'
        }
    
        androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.1'
    
    
    }
    

    希望这肯定会对某人有所帮助,因为即使按照 vogella 教程的完整步骤,我已经挣扎了半天来修复它。

    【讨论】:

      【解决方案6】:

      可能您可能有多个构建类型,默认 Android 项目创建两个构建类型(调试/发布),将构建变体更改为调试或设置值如下

      http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Testing

      Currently only one Build Type is tested. By default it is the debug Build Type, but this can be reconfigured with:
      android {
          ...
          testBuildType "staging"
      }
      

      【讨论】:

        【解决方案7】:

        我遇到了同样的问题,我解决了更改我的 Build Variant 的问题。我在发布版本中运行测试。

        When I changed to "debug", it works

        【讨论】:

        • 哦,哇,我刚刚花了一个小时试图修复我的依赖关系,什么不是!在这种情况下,Intellij 应该提供比“找不到类”更好的反馈。
        【解决方案8】:

        您可以参考这个answer

        “我错误地将测试类放在 src/test 中。将它们移动到 src/androidTest/java/... 之后,依赖关系已解决。也许这也是你的问题。”

        【讨论】:

          猜你喜欢
          • 2017-09-06
          • 2015-08-16
          • 2016-09-29
          • 1970-01-01
          • 2018-11-27
          • 1970-01-01
          • 2023-04-06
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多