我需要从 Android 测试(AndroidJUnit4ClassRunner)中获取资源。推荐的方法是什么,为什么?
String some_res_string =
androidx.test.platform.app.InstrumentationRegistry.getInstrumentation()
.getTargetContext().getString(R.string.some_res_string);
或者
String some_res_string = myActivityTestRule.getActivity() //Activity
.getString(R.string.some_res_string);
完整代码
@RunWith(AndroidJUnit4ClassRunner.class)
public class MyActivityTest {
@Rule
public ActivityTestRule<MyActivity> myActivityTestRule =
new ActivityTestRule<>(MyActivity.class, true, true);
@Test
public void test() throws Exception {
String some_res_string = myActivityTestRule.getActivity() //Activity
.getString(R.string.some_res_string);
String some_res_string2 =
androidx.test.platform.app.InstrumentationRegistry.getInstrumentation()
.getTargetContext().getString(R.string.some_res_string);
}
}