简体   繁体   中英

Android instrumented tests cant find a main resource

I've been banging my head against the wall with this for a while, ive made some progress, but im pretty stuck now.

I've put some android resources in src/androidTest/res , and I'm accessing them in the unit tests I've written just fine.

However it looks like it can't access the resources defined in src/main/res -- they don't appear to being built into the build .R , even though I can access them just fine in Android Studio (no compiler complaints).

Here's more of my setup:

build.gradle:

android {
    compileSdkVersion 23
    buildToolsVersion '23.0.3'

    defaultConfig {
        applicationId "com.my.app"
        minSdkVersion 15
        targetSdkVersion 23
        versionName APP_VERSION
        versionCode getBuildNumber()

        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }

    ...

    buildTypes {
        debug {
            versionNameSuffix "+1-DEBUG"
            applicationIdSuffix ".debug"
        }

        beta {
            versionNameSuffix "+1-BETA"
            applicationIdSuffix ".beta"

            signingConfig signingConfigs.release
        }

        release {
            ...
        }
    }
    ...
}

a test class

import com.my.app.R;
...

public class LaunchActivityTest extends ActivityInstrumentationTestCase2<LaunchActivity> {

    ...

    public LaunchActivityTest() {
        super(LaunchActivity.class);
    }

    @BeforeClass
    @Override
    public void setUp() throws Exception {
        super.setUp();

        injectInstrumentation(InstrumentationRegistry.getInstrumentation());
        LaunchActivity launchActivity = getActivity();

        // read from file
        InputStream inputStream = launchActivity.getResources().openRawResource(
            com.my.app.debug.test.R.raw.test_config);

        ...
    }

    public void testSetUp() {
        ...

        onView(withId(R.id.username_field)).perform(
                typeText(JSONUtils.getString(mJSONConfig, "username")));

the code where i get com.my.app.debug.test.R.raw.test_config runs just fine (no crashes), and that file is in src/androidTest/res/raw/

the test dies when trying to onView(withId(R.id.username_field)... -- that id is in the LaunchActivity's layout in src/main/res/layout/activity_launch.xml .

These tests have run a lot already, so i know they work, everything was in the right spot. I'm moving stuff around because i wanted to get the test resources in the correct spot. Are the resources not merging? do the resource id's not match up now maybe?

java.lang.NullPointerException
at android.support.test.espresso.core.deps.guava.base.Preconditions.checkNotNull(Preconditions.java:210)
at android.support.test.espresso.action.TypeTextAction.<init>(TypeTextAction.java:67)
at android.support.test.espresso.action.TypeTextAction.<init>(TypeTextAction.java:56)
at android.support.test.espresso.action.ViewActions.typeText(ViewActions.java:363)
at com.my.app.package.stuff.LaunchActivityTest.testSetUp(LaunchActivityTest.java:74)
at java.lang.reflect.Method.invoke(Native Method)
at android.test.InstrumentationTestCase.runMethod(InstrumentationTestCase.java:214)

EDIT My problem was a NPE as the accepted answer points to, but here's what was going on. Above, the input stream wasn't reading my resource as plain text. I ended up having to change and use the context from the instrumentation rather than the activity like this:

injectInstrumentation(InstrumentationRegistry.getInstrumentation());
Context context = getInstrumentation().getContext();

// read credentials and org from config file
InputStream inputStream = context.getResources().openRawResource(
    com.spatialnetworks.fulcrum.debug.test.R.raw.test_config);

The reason I wasn't finding it was because I was catching the exception and moving on since it was a unit test and that exception won't really happen... but i was wrong.

This issue is not a missing resource with name R.id.username_field . Instead it comes from a null String (String which should be typed).

Try to check first the if the input string is not null.

String json = JSONUtils.getString(mJSONConfig, "username");
assertNotNull(json)
onView(withId(R.id.username_field)).perform(typeText(json));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM