8

I try to run tests with espresso 2.2.1 in Android Studio 1.5.1. When I run LoginActivityTest I get this error: "android.content.res.Resources$NotFoundException" caused when LoginActivity calls MyService.java and MyService needs a integer resources (i.e. R.integer.number_of_days). This Resources is defined in R.integer.xml file in a gradle ( version 1.5.0) module.

Project's structure:

RootFolder/ ├----projectA/ │ ├----build.gradle │ ├----settings.gradle │ └----src/androidTest/java/.../LoginActivityTest │ └----src/main/java/.../LoginActivity │ └----Module/ ├----krill/ │ └----build.gradle │ ├----settings.gradle │ └----src/main/ | └----java/service/MyService.java | └----res/value/integers.xml │ └----otherModule/ └----build.gradle

My test class:

@RunWith(AndroidJUnit4.class)
@LargeTest
public class LoginActivityTest extends ActivityInstrumentationTestCase2<LoginActivity >{

@Rule
public ActivityTestRule<LoginActivity> mActivityRule = new ActivityTestRule(LoginActivity.class);

public LoginActivityTest() {
    super(LoginActivity.class);
}

@Test
public void testConfigDialog() {
    onView(withId(R.layout.login_custom_view));

    onView(withId(R.id.id_username)).perform(clearText());
    onView(withId(R.id.id_password)).perform(clearText());
}
}

error stacktrace:

Running tests
Test running started
android.content.res.Resources$NotFoundException: Resource ID #0x7f090004
at android.content.res.Resources.getValue(Resources.java:1233)
at android.content.res.Resources.getInteger(Resources.java:989)
at it.company.android.lib.auth.infrastructure.AuthenticatorPreferences.<init>(MyService.java:36)
at it.company.android.lib.auth.infrastructure.AuthenticatorPreferences.getInstance(MyService.java:45)
at it.company.android.lib.auth.application.BaseLoginActivity.onCreate(BaseLoginActivity.java:27)
at it.company.android.novae.application.LoginActivity.onCreate(LoginActivity.java:57)
at android.app.Activity.performCreate(Activity.java:5937)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.support.test.runner.MonitoringInstrumentation.callActivityOnCreate(MonitoringInstrumentation.java:534)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)


java.lang.RuntimeException: Unable to start activity ComponentInfo{it.company.android.novae/it.company.android.appname.application.LoginActivity}: android.content.res.Resources$NotFoundException: Resource ID #0x7f090004
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x7f090004
at android.content.res.Resources.getValue(Resources.java:1233)
at android.content.res.Resources.getInteger(Resources.java:989)
at it.company.android.lib.auth.infrastructure.AuthenticatorPreferences.<init>(AuthenticatorPreferences.java:36)
at it.company.android.lib.auth.infrastructure.AuthenticatorPreferences.getInstance(MyService.java:45)
at it.company.android.lib.auth.application.BaseLoginActivity.onCreate(BaseLoginActivity.java:27)
at it.company.android.novae.application.LoginActivity.onCreate(LoginActivity.java:57)
at android.app.Activity.performCreate(Activity.java:5937)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.support.test.runner.MonitoringInstrumentation.callActivityOnCreate(MonitoringInstrumentation.java:534)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
... 10 more

Test running failed: Instrumentation run failed due to 'android.content.res.Resources$NotFoundException'

How can I fix this problem?

capo11
  • 790
  • 3
  • 11
  • 32
Insoft
  • 548
  • 6
  • 21
  • And it works fine if you perform the same action manually (by yourself)? – Sufian Feb 11 '16 at 14:33
  • If I run the app (without test), all works fine. I'm not sure if I answer your question. – Insoft Feb 11 '16 at 14:36
  • I am getting same error after updating to `classpath 'com.android.tools.build:gradle:2.0.0-beta3'`. Seems Google just released it. yesterday I didn't have this problem :( – Hesam Feb 11 '16 at 19:26
  • @Insoft I am getting this same error trying to make an Instrumentation test, for a project that includes a module, like yours. Did you figure out how to get test to run? – Matthew Carlson Mar 01 '16 at 16:01
  • @Insoft I figured out my problem: the R.java are not created correctly if I have `androidTestCompile project(':LibraryModule')`. When I removed that, it worked. I was still able to use the library in my instrumentation tests. – Matthew Carlson Mar 01 '16 at 17:54
  • R.layout.view? Nope, R.id.view - denys mentioned it below as well – Saik Caskey Apr 20 '17 at 09:43

2 Answers2

1

If you use JUnit4 in your test and ActivityTestRule then you don't need to extend ActivityInstrumentationTestCase2<LoginActivity> and don't need constructor. Fix your code so it will look like this and try out again:

@RunWith(AndroidJUnit4.class)
@LargeTest
public class LoginActivityTest {

    @Rule
    public ActivityTestRule<LoginActivity> mActivityRule = new ActivityTestRule(LoginActivity.class);

    @Test
    public void testConfigDialog() {
        onView(withId(R.layout.login_custom_view)); //you can't use layout id here there must be a view id

        onView(withId(R.id.id_username)).perform(clearText());
        onView(withId(R.id.id_password)).perform(clearText());
    }
}

UPDATE: see comment in the code

denys
  • 6,834
  • 3
  • 37
  • 36
  • take a look on my update, you can't use layout id in the `onView()`. Moreover it doesn't have any action like click or check. Remove that line. – denys Feb 11 '16 at 15:49
  • Ok, I removed that line and I get the same error. I think because is the ActivityTestRule inizialize LoginActivity that calls MySerivce. So the problem is before run testConfigDialog method. – Insoft Feb 11 '16 at 16:09
  • I think the problem is, when I launch test, the context don't get (or is invisible) the right resource file, or something like that. It could be? – Insoft Feb 11 '16 at 16:14
0

I had the same problem, the problem is in build.gradle

I downgrade espresso

androidTestCompile 'com.android.support.test:runner:0.4.1'
androidTestCompile 'com.android.support.test:rules:0.4.1'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.1'
Cabezas
  • 9,329
  • 7
  • 67
  • 69