androidtestingjunitresources

How can I access a string resource from a test?


I have a project in android. I want to test it in junit. In the resources, insinide strings.xml I have a string array called labels_array. How can I access (get) this string array from a test method in junit?

In the test class I have

    @Rule
    public ActivityScenarioRule mActivityRule = new ActivityScenarioRule<>(
            MainActivity.class);

and


    @Test
    public void fooTest() {
        ActivityScenario scenario = mActivityRule.getScenario();
}

But How can I use these rule and method in order to acess the string array from inside the method fooTest?


Solution

  • Since you want to obtain the real value of the string array, you can use:

    final Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();
    @ArrayRes final String[] labelsArray = context.getResources().getStringArray(R.array.labels_array);