【问题标题】:Android instrumented tests cant find a main resourceAndroid 仪器测试找不到主要资源
【发布时间】:2016-05-26 21:06:06
【问题描述】:

一段时间以来,我一直在用头撞墙,我取得了一些进展,但现在我陷入了困境。

我已经在src/androidTest/res 中放置了一些android 资源,并且我正在我编写的单元测试中访问它们。

但它看起来无法访问src/main/res 中定义的资源——它们似乎没有内置到构建.R 中,尽管我可以在 Android Studio 中正常访问它们(没有编译器投诉)。

这是我的更多设置:

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 {
            ...
        }
    }
    ...
}

一个测试类

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")));

我得到com.my.app.debug.test.R.raw.test_config 的代码运行良好(没有崩溃),并且该文件位于src/androidTest/res/raw/

在尝试onView(withId(R.id.username_field)... 时测试会终止——该 id 在 src/main/res/layout/activity_launch.xml 的 LaunchActivity 布局中。

这些测试已经运行了很多,所以我知道它们有效,一切都在正确的位置。我正在移动东西,因为我想将测试资源放在正确的位置。资源不合并?资源ID现在可能不匹配吗?

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)

编辑 正如公认的答案所指出的那样,我的问题是 NPE,但这就是正在发生的事情。上面,输入流没有将我的资源作为纯文本读取。我最终不得不更改和使用仪表中的上下文,而不是像这样的活动:

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);

我没有找到它的原因是因为我正在捕获异常并继续前进,因为它是一个单元测试并且该异常不会真正发生......但我错了。

【问题讨论】:

    标签: android android-resources android-testing android-espresso r.java-file


    【解决方案1】:

    此问题不是名称为 R.id.username_field 的缺失资源。相反,它来自一个空字符串(应该输入的字符串)。

    尝试先检查输入字符串是否为空。

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

    【讨论】:

    • 你是对的。有时,当您处理一个问题很长时间时,您会忘记查看正在发生的其他事情,从而变得专注..谢谢!很抱歉浪费您的时间。我现在的问题似乎是由于某种原因src/androidTest/res/raw 中的资源没有以纯文本形式打开;就是这样:����D����������������——如果我想不通,可能需要另一个 SO 帖子
    猜你喜欢
    • 1970-01-01
    • 2016-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多