I'm currently running into trouble retrieving <meta-data>
tags from my AndroidManifest file while being inside an InstrumentationTest.
I am using a library (Sugar ORM) which stores some essential information inside these tags. 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()
.
The problem is that InstrumentationTestCase
does not contain the meta data from your application. All you need to do is extend ApplicationTestCase<Application>
for your test case class.
You can call getContext() in your test cases and that will have the appropriate information in there.
PackageManager pm = getContext().getPackageManager();
ApplicationInfo ai = pm.getApplicationInfo(getContext().getPackageName(), PackageManager.GET_META_DATA);
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments