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

时间:2016-08-03 21:06:34

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

我正在尝试为我的Android应用编写测试测试。当我运行我的测试时,它会触及我的代码中试图使用 com.google.android.gms.vision.face.FaceDetector 库的部分,测试框架会抛出以下错误:< / p>

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答案的镜像,所有人都说将元数据标签添加到清单(this one, for example),但是我已经添加了它,测试仍然不会通过FaceDetector库运行。

1 个答案:

答案 0 :(得分:1)

发现问题

测试运行器上下文没有清单文件。我需要将正确的上下文传递给我正在测试的方法。我的测试现在看起来像这样(注意交换到 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,上下文列表对我有帮助。