Hi Folks When I run unit tests in Android Studio ...
# multiplatform
h
Hi Folks When I run unit tests in Android Studio using Robolectric, I am encountering the following error:
java.lang.ExceptionInInitializerError
Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x7f14001d
at org.robolectric.shadows.ShadowLegacyAssetManager.getResName(ShadowLegacyAssetManager.java:1378)
at org.robolectric.shadows.ShadowLegacyAssetManager.resolveResourceValue(ShadowLegacyAssetManager.java:1152)
at org.robolectric.shadows.ShadowLegacyAssetManager.resolve(ShadowLegacyAssetManager.java:1112)
at org.robolectric.shadows.ShadowLegacyAssetManager.getAndResolve(ShadowLegacyAssetManager.java:1106)
at org.robolectric.shadows.ShadowLegacyAssetManager.getResourceText(ShadowLegacyAssetManager.java:282)
at android.content.res.AssetManager.getResourceText(AssetManager.java)
at android.content.res.Resources.getText(Resources.java:225)
at android.content.res.Resources.getString(Resources.java:313)
at android.content.Context.getString(Context.java:292)
at com.xyz.UtilsKt.getOauthClientId(Utils.kt:48)
The contents of Utils.Kt is:
Copy code
import android.content.Context
import androidx.test.core.app.ApplicationProvider


internal var appContext: Context? = ApplicationProvider.getApplicationContext()

internal actual fun getOauthClientId() : String = appContext?.getString(R.string.oauth_client_id) ?: ""
I believe there is an issue accessing R.string.oauth_client_id. Any suggestions on how to resolve this issue? Thanks in advance.
c
I guess you followed the setup guide and set
testOptions.unitTests.isIncludeAndroidResources
? 😉 https://developer.android.com/training/testing/local-tests/robolectric#ui-testing
also this in neither a multiplatform nor a Kotlin question. For those type of questions you should find better resources to ask. in the #C0B8M7BUY channels description you can find some links. 👍🏼
h
Thanks for your response 👍. I somehow missed this configuration. Adding this seems to have fixed the issue for me. I apologise for posting the query in the wrong channel. Will keep it in mind next time.
I had one more query. I have set testOptions.unitTests.isIncludeAndroidResources = true Is there a way to manually override AndroidManifest.xml while running tests using Robolectric? There is a code snippet in the AndroidManifest file that causes Robolectric to fail.
<receiver
android:name=".appBeacon.StartupBroadcastReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
I tried overriding the manifest using
@Config(manifest = '../path/to/customManifest.xml')
However, this setting didn't work.
c
please ask in a Android Forum 😉
j
If this is an error resolving an android string resource in a test perhaps its the issue that resources aren't included by androidTestImplementation dependencies. Try adding a debugImplementation dependency on whatever module this string lives in, in your app module.