首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Android Espresso :无法解析符号AndroidJUnit4.class

Android Espresso :无法解析符号AndroidJUnit4.class
EN

Stack Overflow用户
提问于 2015-08-24 14:38:06
回答 6查看 26.5K关注 0票数 23

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

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

代码语言:javascript
复制
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> {

}

我总是收到这样的错误消息:

代码语言:javascript
复制
cannot resolve symbol AndroidJUnit4.class

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

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

代码语言:javascript
复制
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

我试着通过下面的问题来解决这个问题:Cannot resolve symbol 'AndroidJUnit4'

但是没有那么幸运。

非常感谢您的建议。

EN

回答 6

Stack Overflow用户

发布于 2015-09-07 21:26:46

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

然后我找到了罗杰·胡最近更新的另一个教程"UI Testting with Espresso“。我注意到一个评论说Espresso还不支持棉花糖。

添加依赖项的方式如下:

代码语言:javascript
复制
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'
}

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

票数 14
EN

Stack Overflow用户

发布于 2016-02-11 17:11:43

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

代码语言:javascript
复制
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;
票数 11
EN

Stack Overflow用户

发布于 2015-11-09 11:29:16

根据上面给出的gradle更改:

代码语言:javascript
复制
androidTestCompile 'com.android.support.test:runner:0.3'

您需要更改为

代码语言:javascript
复制
androidTestCompile('com.android.support.test:runner:0.3') {
    exclude group: 'com.android.support', module: 'support-annotations'
}

对我来说,即使在上面的更改中它也不起作用,所以我注意到我遗漏了下面的内容:

代码语言:javascript
复制
androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.1'

对我来说效果很好。

完整的build.gradle如下所示:

代码语言:javascript
复制
    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教程的完整步骤之后也是如此。

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32175847

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档