工具测试 - setContentView()

时间:2016-08-01 19:33:07

标签: android testing nullpointerexception android-espresso

我在app/androidTest/java/myapp进行了一次仪器测试。我有一个简单的测试方法:

public class LoginActivityTest {

    private static final String EMAIL = "admin@gmail.com";
    private static final String PASSWORD = "admin";

    @Rule
    public IntentsTestRule<LoginActivity> loginActivity = new IntentsTestRule<>(LoginActivity.class);

    @Test
    public void LoginWithProperCredentials() {
        onView(withId(R.id.email_textView))
                .perform(typeText(EMAIL));
        onView(withId(R.id.password_login))
                .perform(typeText(PASSWORD));
        onView(withId(R.id.email_sign_in_button))
                .perform(click());

        intended(allOf(
                hasExtra(GeneralConstants.AUTHENTICATED, true),
                hasExtra(GeneralConstants.TOKEN, isA(String.class)),
                toPackage("myapp")));
    }
}

我已升级Android支持存储库,添加了必要的androidTestCompile依赖项以及默认的testInstrumentationRunner("android.support.test.runner.AndroidJUnitRunner"),并且我已选择Android Instrumentation Tests作为构建变体。然而,尝试启动测试会给我ResourceNotFoundException。我已经发现,setContentView()中的onCreate(Bundle bundle)方法会抛出此异常。

我现在正在努力解决这个问题。可能是什么原因?

修改

我已检查过myapp.test.R中是否有遗失的字段导致ResourceNotFoundException。此字段也在myapp.R中,但它具有与之相关的不同值(test.R中的0x7f040015和0x7f030015 - 缺少此值)。我试图明确导入:

import pl.kawowydzienniczek.kawowydzienniczek.test.R; 

但后来试着写:

onView(withId(R.id.email_textView))
                .perform(typeText(EMAIL));

错误地告诉我email_textView无法解决。 应该怎么做?

EDIT2:

我注意到评论了以下依赖

androidTestCompile 'com.android.support.test.espresso:espresso-contrib:2.2.2'

让它发挥作用。现在它应该没问题,但为什么这样做以及如何在不丢弃该库的情况下制作浓缩咖啡呢?

1 个答案:

答案 0 :(得分:0)

根据

  

现在它应该没问题,但为什么会这样,以及如何制作浓缩咖啡   没有扔出那个图书馆的工作?

这是我的build.gradle配置:

dependencies {
    ext.JUNIT_VERSION = '4.12'
    ext.SUPPORT_VERSION = '24.1.1'
    ext.ESPRESSO_VERSION = '2.2.2'

    compile fileTree(dir: ulibs', include: ['*.jar'])
    testCompile "junit:junit:$JUNIT_VERSION"
    testCompile("org.robolectric:robolectric:3.0") {
        exclude module: 'classworlds'
        exclude module: 'commons-logging'
        exclude module: 'httpclient'
        exclude module: 'maven-artifact'
        exclude module: 'maven-artifact-manager'
        exclude module: 'maven-error-diagnostics'
        exclude module: 'maven-model'
        exclude module: 'maven-project'
        exclude module: 'maven-settings'
        exclude module: 'plexus-container-default'
        exclude module: 'plexus-interpolation'
        exclude module: 'plexus-utils'
        exclude module: 'wagon-file'
        exclude module: 'wagon-http-lightweight'
        exclude module: 'wagon-provider-api'
    }

    compile "com.android.support:appcompat-v7:$SUPPORT_VERSION"
    compile "com.android.support:design:$SUPPORT_VERSION"

    androidTestCompile 'com.android.support.test:runner:0.5'
    androidTestCompile "com.android.support:support-annotations:$SUPPORT_VERSION"
    androidTestCompile "com.android.support.test.espresso:espresso-core:$ESPRESSO_VERSION"
    androidTestCompile "com.android.support.test.espresso:espresso-intents:$ESPRESSO_VERSION"
    /**
     * AccessibilityChecks
     * CountingIdlingResource
     * DrawerActions
     * DrawerMatchers
     * PickerActions (Time and Date picker)
     * RecyclerViewActions
     */
    androidTestCompile("com.android.support.test.espresso:espresso-contrib:$ESPRESSO_VERSION") {
        exclude group: 'com.android.support', module: 'appcompat'
        exclude group: 'com.android.support', module: 'support-v4'
        exclude group: 'com.android.support', module: 'support-v7'
        exclude group: 'com.android.support', module: 'design'
        exclude module: 'support-annotations'
        exclude module: 'recyclerview-v7'
    }
    compile "junit:junit:${JUNIT_VERSION}"
}

尝试排除与我已经完成的相同的组模块和组。

检查:https://github.com/piotrek1543/LocalWeather/blob/master/app/build.gradle

希望它会有所帮助