Android Studio 3.1. , Java 1.8. , Gradle 4.4.
Here my Espresso test:
@RunWith(AndroidJUnit4.class)
public class SignInActivityTest {
private Context context;
@Rule
public ActivityTestRule<SignInActivity> mActivityRule = new ActivityTestRule<>(SignInActivity.class, true, false);
private MockWebServer server;
@Before
public void init() throws IOException {
context = getInstrumentation().getContext();
Intent intent = new Intent();
mActivityRule.launchActivity(intent);
}
@Test
public void loginTextViewText() {
String loginString = context.getResources().getString(R.string.login).toUpperCase();
onView(withId(R.id.loginTextView)).check(matches(withText(loginString)));
}
}
But when I try to start test loginTextViewText()
I get error:
android.content.res.Resources$NotFoundException: String resource ID
0x7f0e004f
at android.content.res.Resources.getText(Resources.java:312) at android.content.res.Resources.getString(Resources.java:400) at com.myproject.android.activity.SignInActivityTest.loginTextViewText(SignInActivityTest.java:106) at java.lang.reflect.Method.invoke(Native Method) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
How I can fix this? Thanks.
If anyone is still struggling on this one, I fixed it using targetContext :
context = InstrumentationRegistry.getInstrumentation().targetContext
Because for some reason, sometimes activityRule.activity was null.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With