Android Espresso : cannot resolve symbol AndroidJUnit4. 类

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

我正在尝试在新的 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'

但没有运气。

非常感谢您的任何建议。

最佳答案

我也尝试过 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'
}

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

关于Android Espresso : cannot resolve symbol AndroidJUnit4. 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32175847/

相关文章:

python - 如何从 PySide 中的 .ui 文件加载子项?

android - 如果android :interpolator is unspecified,默认插值器是什么

java - 在特定模式之后提取字符串

android - Google Play 中的自动更新会降级包吗?

unit-testing - 在域类上模拟 “save”方法

reactjs - Typescript + React 测试库 - 'SidebarItem' 指的是一个值,但在这里被用作类型。你的意思是 'typeof SidebarItem' ?ts(2749)

c# - Linq Join 的摩尔

c# - 如何在 c#/.net 中实现编辑 -> 复制菜单

android - 使用 Android YouTubePlayer 的 Vevo 内容

java - 如何更新Java swing背景图?