Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android. Espresso: android.content.res.Resources$NotFoundException: String resource ID

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.

like image 812
Alexei Avatar asked Mar 06 '23 11:03

Alexei


1 Answers

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.

like image 165
Benoist Laforge Avatar answered Apr 06 '23 02:04

Benoist Laforge