Issue
I'm getting android.content.res.Resources$NotFoundException: String resource ID when trying to access resources defined in the androidTest/res during runtime on my tests. I've tried a couple of things and no luck. Isn't this supposed to work? My code sample:
ExampleInstrumentedTest.kt
@RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTest {
@Test
fun test() {
val result = InstrumentationRegistry.getInstrumentation().targetContext.getString(
com.myproject.test.R.string.my_test
)
assertEquals("my test", result)
}
}
strings.xml
<resources>
<string name="my_test">my test</string>
</resources>
Has anyone faced this issue?
Solution
Found the solution for this. The issue at hand is that InstrumentationRegistry.getInstrumentation().targetContext will only be able to access what's included in the application being tested, so in this case I should use InstrumentationRegistry.getInstrumentation().context, which will be able to access test resources. For reference:
getTargetContext Return a Context for the target application being instrumented. Note that this is often different than the Context of the instrumentation code, since the instrumentation code often lives is a different package than that of the application it is running against. See getContext() to retrieve a Context for the instrumentation code.
getContext Return the Context of this instrumentation's package. Note that this is often different than the Context of the application being instrumentated, since the instrumentation code often lives is a different package than that of the application it is running against. See getTargetContext() to retrieve a Context for the target application.
from doc: https://developer.android.com/reference/android/app/Instrumentation
Answered By - william xyz
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.