我有一个安卓项目。我想在 junit 中测试它。在资源中,在 strings.xml
中,我有一个名为 labels_array
的字符串数组。我如何从 junit 中的测试方法访问(获取)这个字符串数组?
在我的测试课上
@Rule
public ActivityScenarioRule mActivityRule = new ActivityScenarioRule<>(
MainActivity.class);
和
@Test
public void fooTest() {
ActivityScenario scenario = mActivityRule.getScenario();
}
但是我如何使用这些规则和方法来从方法 fooTest
中访问字符串数组?
最佳答案
既然要获取字符串数组的真实值,可以使用:
final Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();
@ArrayRes final String[] labelsArray = context.getResources().getStringArray(R.array.labels_array);
关于android - 如何从测试中访问字符串资源?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58493104/