为什么官方文档中的Android Instrumented Test示例不起作用?

时间:2018-07-03 23:44:58

标签: android unit-testing automated-tests

根据https://developer.android.com/training/testing/unit-testing/instrumented-unit-tests上的文档,我为我的应用模块创建了以下build.gradle文件:

buildscript {
    repositories {
        mavenCentral()
        maven { url 'https://maven.fabric.io/public' }
    }

    dependencies {
        classpath 'io.fabric.tools:gradle:1.25.3'
    }
}
apply plugin: 'com.android.application'
apply plugin: 'io.fabric'

repositories {
    maven { url 'https://maven.fabric.io/public' }
}


android {

    compileSdkVersion 27
    buildToolsVersion '27.0.3'
    useLibrary 'org.apache.http.legacy'
    defaultConfig {
        applicationId "com.myapp"
        minSdkVersion 19
        targetSdkVersion 19
        versionCode 7
        versionName "1.3.0"
        ndk {
            abiFilters "armeabi", "armeabi-v7a", "x86", "mips"
        }

        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }

    sourceSets {
        main {
            jniLibs.srcDirs = ['libs']
        }
    }
    lintOptions {
        checkReleaseBuilds false
        abortOnError false
    }
    dexOptions {
        javaMaxHeapSize "4g"
    }
}

dependencies {
    // Required -- JUnit 4 framework
    testImplementation 'junit:junit:4.12'
    // Optional -- Mockito framework
    testImplementation 'org.mockito:mockito-core:1.10.19'
    androidTestImplementation 'com.android.support:support-annotations:27.1.1'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test:rules:1.0.2'
    androidTestImplementation 'org.hamcrest:hamcrest-library:1.3'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-contrib:3.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-intents:3.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-accessibility:3.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-web:3.0.2'
    androidTestImplementation 'com.android.support.test.espresso.idling:idling-concurrent:3.0.2'

    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation project(':deviceprint-lib-2.0.1')
    implementation files('libs/gson-2.2.4.jar')
    implementation files('libs/activation.jar')
    implementation files('libs/additionnal.jar')
    implementation files('libs/mail.jar')

    implementation 'com.android.support:support-v4:27.1.1'

}

和以下项目级别build.gradle:

buildscript {
    repositories {
        jcenter()
        google()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.1.2'
    }
}

allprojects {
    repositories {
        jcenter()
        google()
    }
}

我在app / src / androidTest / java / com / mypackage中创建了一个目录,并添加了以下文件:

package com.mypackage;

import android.support.test.runner.AndroidJUnit4;
import org.junit.Test;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;


public class ApplicationTest
{
    public ApplicationTest()
    {

    }

    @Test public void failingTest()
    {
        assertThat(false, is(true));
    }
}

所有导入语句和@Test注释都标记为错误,并显示消息“无法解析符号”,如果我在项目窗口中右键单击该文件,则没有“运行”选项。 ”。为什么?

1 个答案:

答案 0 :(得分:1)

重新启动Android Studio后,该示例开始正常工作。 :facepalm:

编辑: 另一个注意事项是,由于不同的原因,我最近遇到了这些相同的症状:必须将build变量设置为“调试”(或者必须在模块{{1}的android {}块内设置testBuildType }文件添加到您要测试的变体中