How can I access a string resource from a test?
Asked Answered
H

3

6

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?

Honorine answered 21/10, 2019 at 19:41 Comment(0)
C
11

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);
Census answered 21/10, 2019 at 21:14 Comment(8)
It solved my problem, thanks! But now I get a warning about context that says "access can be provate".Honorine
That's because probably you defined it global. You can use the visibility you prefer (e.g. making it private)Census
Actually now I see that the first line works but the command context.getResources() throws an exception: ``` E/TestRunner: failed: iterateSpinnerItems(android.example.phonenumberspinnerWithTests.SpinnerSelectionTest) ----- begin exception ----- E/TestRunner: android.content.res.Resources$NotFoundException: String array resource ID #0x7f020000 ```Honorine
Are you running your tests under androidTest?Census
Yes, it is in androidTest. Also, If it is matter somehow, I go according to the project and the test in section 3 in this tutorial (where I changed a little bit so it will work with newer API): google-developer-training.github.io/…Honorine
I don't think I import the R file explicitely. My imports are: android.content.Context, androidx..., org.junit..., androidx.test... and org.hamcrest.Matchers....Honorine
Let us continue this discussion in chat.Census
After discussion @Giorgio turn my attension that I used getContext() instead of getTargetContext() and that solve the problem.Honorine
B
1

You can do this with mock.I think one of below links may be the solution to your request.

https://medium.com/android-testing-daily/unit-testing-xml-resources-7387447f9ef7

or

Unit test Android, getString from resource

Boreas answered 21/10, 2019 at 20:6 Comment(0)
O
0

It depends which source-set one intends to access. In order to load strings eg. from androidTest/res/strings.xml, one has to import class .test.R instead of class .R.

Then one can access the resources with .getContext() instead of .getTargetContext():

@RunWith(AndroidJUnit4.class)
public class SomeTest extends TestCase {
    String publicKey = null;
    @Before
    public void setupTest() {
        Context context = InstrumentationRegistry.getInstrumentation().getContext();
        this.publicKey = context.getString(R.string.some_public_key);
    }
    @Test
    ...
}
Overripe answered 22/4 at 3:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.