由于 com.google.android.gms.version 元数据导致的 Android 仪器测试错误

标签 android unit-testing testing android-camera android-vision

我正在尝试为我的 Android 应用程序编写仪器测试。当我运行测试时,它命中了我的代码中尝试使用 com.google.android.gms.vision.face.FaceDetector 库的部分,测试框架抛出以下错误:

A required meta-data tag in your app's AndroidManifest.xml does not exist.  You must have the following declaration within the <application> element:     <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />

但是,我的 AndroidManifest.xml 已经包含那个确切的元数据标签,就在这里:

<application
        android:allowBackup="true"
        android:hardwareAccelerated="true"
        android:icon="@drawable/icon"
        android:label="NeuralEye-FaceID-2"
        android:theme="@style/Theme.AppCompat"
        android:largeHeap="true">
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />
        <meta-data
            android:name="com.google.android.gms.vision.DEPENDENCIES"
            android:value="face" />

我错过了什么?我的仪器测试看起来像这样:

@RunWith(AndroidJUnit4.class)
public class FeatureExtractionTest {


    public FeatureExtraction mFeatureExtraction;

    public ConcurrentSkipListSet<String> skipList = new ConcurrentSkipListSet();

    @Before
    public void createFeatureExtraction() {
        mFeatureExtraction = new FeatureExtraction();
    }

    @Test
    public void testGetFeatures() throws Exception {
        File file = new File("new.txt");
        Context ctx = InstrumentationRegistry.getContext();
        mFeatureExtraction.getFeatures(file, skipList, ctx);
    }
}

编辑:添加 build.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "22.0.1"

    defaultConfig {
        applicationId "tuan.search"
        minSdkVersion 21
        targetSdkVersion 23
        versionCode 1
        versionName "0.5 "
        multiDexEnabled true
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile files('libs/opencsv-3.7.jar')
    compile 'com.android.support:support-v4:23.4.0'
    compile 'com.google.android.gms:play-services:9.0.1'
    compile 'com.android.support:design:23.4.0'
    compile 'org.projectlombok:lombok:1.16.8'
    compile 'com.android.support:multidex:1.0.0'
    compile 'org.apache.directory.studio:org.apache.commons.io:2.4'
    testCompile 'junit:junit:4.12'
    testCompile 'org.mockito:mockito-core:1.10.19'
    androidTestCompile 'com.android.support.test:runner:0.4'
    androidTestCompile 'com.android.support.test:rules:0.4'
    androidTestCompile 'com.android.support:support-annotations:23.4.0'
    androidTestCompile 'org.hamcrest:hamcrest-library:1.3'
    androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
    androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.2'
}

感谢任何帮助。我已经找到了无数的 SO 答案,它们都说将元数据标记添加到 list 中(this one, for example),但是我已经添加了它并且测试仍然不会通过 FaceDetector 库。

最佳答案

发现问题

测试运行器上下文没有 list 文件。我需要将正确的上下文传递到我正在测试的方法中。我的测试现在看起来像这样(注意交换到 getTargetContext):

@Test
public void testGetFeatures() throws Exception {
    File file = new File("new.txt");
    Context ctx = InstrumentationRegistry.getTargetContext();
    mFeatureExtraction.getFeatures(file, skipList, ctx);
}

FaceDetector 能够被初始化。现在我可以完成实际实现我的测试了。希望这对某人有所帮助。

支持 this random answer ,上下文列表对我有帮助。

关于由于 com.google.android.gms.version 元数据导致的 Android 仪器测试错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38753637/

相关文章:

android - 在 keystore 中找不到具有别名的 key

android - 在 xml 中的 "include"布局中引用 View 的 id

java - 如何对创建新对象的类进行单元测试

regex - 使用正则表达式匹配 URL

android - 在该行中单击按钮时确定行号

android - 使用机器人框架脚本和 chromedriver 在 Android 设备中打开 Chrome 浏览器?

javascript - 快照时间戳不匹配

c# - Moq 上的扩展方法返回 null

android - 测试使用 Messenger 的远程服务

Spring - 在测试中替换@Service bean