简体   繁体   English

从UnitTest中的清单中获取元数据

[英]Get meta-data from manifest in UnitTest

I'm currently running into trouble retrieving <meta-data> tags from my AndroidManifest file while being inside an InstrumentationTest. 我目前在进入InstrumentationTest时从我的AndroidManifest文件中检索<meta-data>标签时遇到麻烦。

I am using a library ( Sugar ORM ) which stores some essential information inside these tags. 我正在使用一个库( Sugar ORM ),该库在这些标签中存储一些基本信息。 As soon as I use the library inside the code to be tested I run into problems. 一旦我在要测试的代码中使用该库,就会遇到问题。

AndroidManifest.xml

<manifest package="org.something" ...>
    ...
    <application ...>
        <meta-data android:name="DATABASE" android:value="foo.db" />
        ...
    </application>
</manifest>

Retrieval of the metadata works like that: 检索元数据的方式如下:

PackageManager pm = context.getPackageManager();
try {
    ApplicationInfo ai = pm.getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA);
    value = ai.metaData.getString(name);
} catch (Exception e) {
    Log.d("sugar", "Couldn't find config value: " + name);
}

The test is running as a InstrumentationTestCase and sets up the context to be getInstrumentation().getContext() . 该测试以InstrumentationTestCase形式运行,并将上下文设置为getInstrumentation().getContext()

I know that this is quite old, however if anyone else is interested, I had this same problem I fixed it by moving the meta-data fields into the application. 我知道这已经很老了,但是如果其他人感兴趣,我也会遇到同样的问题,我通过将元数据字段移到应用程序中来解决了这个问题。

So this: 所以这:

<?xml version="1.0" encoding="utf-8"?>
<manifest
package="com.percolate.coffee"
xmlns:android="http://schemas.android.com/apk/res/android">

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

<meta-data android:name="DATABASE" android:value="coffee.db" />
<meta-data android:name="VERSION" android:value="2" />
<meta-data android:name="QUERY_LOG" android:value="true" />
<meta-data android:name="DOMAIN_PACKAGE_NAME" android:value="com.percolate" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme"
    android:name="com.orm.SugarApp">
    <activity
        android:name=".activity.CoffeeTypeListViewActivity"
        android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>

            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>


    <activity
        android:name=".activity.CoffeeTypeDetailedViewAcitivity"
        android:label="@string/title_activity_coffee_type_detailed_view_acitivity"
        android:parentActivityName=".activity.CoffeeTypeListViewActivity">

    </activity>
    <!-- SERVICES -->
    <service
        android:name="com.octo.android.robospice.Jackson2SpringAndroidSpiceService"
        android:exported="false"/>
    <service
        android:name="com.octo.android.robospice.spicelist.okhttp.OkHttpBitmapSpiceService"
        android:exported="false"/>

</application>

becomes this: 变成这个:

<?xml version="1.0" encoding="utf-8"?>
<manifest
package="com.percolate.coffee"
xmlns:android="http://schemas.android.com/apk/res/android">

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

<meta-data android:name="DATABASE" android:value="coffee.db" />
<meta-data android:name="VERSION" android:value="2" />
<meta-data android:name="QUERY_LOG" android:value="true" />
<meta-data android:name="DOMAIN_PACKAGE_NAME" android:value="com.percolate" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme"
    android:name="com.orm.SugarApp">
    <activity
        android:name=".activity.CoffeeTypeListViewActivity"
        android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>

            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>


    <activity
        android:name=".activity.CoffeeTypeDetailedViewAcitivity"
        android:label="@string/title_activity_coffee_type_detailed_view_acitivity"
        android:parentActivityName=".activity.CoffeeTypeListViewActivity">

    </activity>
    <!-- SERVICES -->
    <service
        android:name="com.octo.android.robospice.Jackson2SpringAndroidSpiceService"
        android:exported="false"/>
    <service
        android:name="com.octo.android.robospice.spicelist.okhttp.OkHttpBitmapSpiceService"
        android:exported="false"/>

</application>

The problem is that InstrumentationTestCase does not contain the meta data from your application. 问题是InstrumentationTestCase不包含您的应用程序中的元数据。 All you need to do is extend ApplicationTestCase<Application> for your test case class. 您需要做的就是为您的测试用例类扩展ApplicationTestCase<Application>

You can call getContext() in your test cases and that will have the appropriate information in there. 您可以在测试用例中调用getContext(),然后在其中包含适当的信息。

PackageManager pm = getContext().getPackageManager();
ApplicationInfo ai = pm.getApplicationInfo(getContext().getPackageName(), PackageManager.GET_META_DATA);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM