Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 975
J. Doe Avatar asked Jan 26 '23 17:01

J. Doe


1 Answers

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);
like image 177
Giorgio Antonioli Avatar answered Feb 01 '23 19:02

Giorgio Antonioli