【问题标题】:NullPointer Exception when using Espresso使用 Espresso 时出现 NullPointer 异常
【发布时间】:2013-10-24 06:30:46
【问题描述】:

我正在尝试使用 Espresso。但是我收到了这个错误,如果我错过了一些代码,我就没有。

任何想法都将受到高度赞赏。

错误日志:

java.lang.NullPointerException: No instrumentation registered. Must run under a registering instrumentation.
    at com.google.android.apps.common.testing.testrunner.util.Checks.checkNotNull(Checks.java:28)
    at com.google.android.apps.common.testing.testrunner.InstrumentationRegistry.getInstance(InstrumentationRegistry.java:23)
    at com.google.android.apps.common.testing.ui.espresso.base.BaseLayerModule.provideTargetContext(BaseLayerModule.java:50)
    at com.google.android.apps.common.testing.ui.espresso.base.BaseLayerModule$$ModuleAdapter$ProvideTargetContextProvidesAdapter.get(BaseLayerModule$$ModuleAdapter.java:101)
    at com.google.android.apps.common.testing.ui.espresso.base.BaseLayerModule$$ModuleAdapter$ProvideTargetContextProvidesAdapter.get(BaseLayerModule$$ModuleAdapter.java:85)
    at com.google.android.apps.common.testing.ui.espresso.base.DefaultFailureHandler$$InjectAdapter.get(DefaultFailureHandler$$InjectAdapter.java:53)
    at com.google.android.apps.common.testing.ui.espresso.base.DefaultFailureHandler$$InjectAdapter.get(DefaultFailureHandler$$InjectAdapter.java:20)
    at com.google.android.apps.common.testing.ui.espresso.base.BaseLayerModule$$ModuleAdapter$ProvideFailureHanderProvidesAdapter.get(BaseLayerModule$$ModuleAdapter.java:555)
    at com.google.android.apps.common.testing.ui.espresso.base.BaseLayerModule$$ModuleAdapter$ProvideFailureHanderProvidesAdapter.get(BaseLayerModule$$ModuleAdapter.java:519)
    at com.google.android.apps.common.testing.ui.espresso.base.BaseLayerModule$FailureHandlerHolder$$InjectAdapter.get(BaseLayerModule$FailureHandlerHolder$$InjectAdapter.java:53)
    at com.google.android.apps.common.testing.ui.espresso.base.BaseLayerModule$FailureHandlerHolder$$InjectAdapter.get(BaseLayerModule$FailureHandlerHolder$$InjectAdapter.java:20)
    at dagger.internal.Linker$SingletonBinding.get(Linker.java:327)
    at com.google.android.apps.common.testing.ui.espresso.base.BaseLayerModule$$ModuleAdapter$ProvideFailureHandlerProvidesAdapter.get(BaseLayerModule$$ModuleAdapter.java:505)
    at com.google.android.apps.common.testing.ui.espresso.base.BaseLayerModule$$ModuleAdapter$ProvideFailureHandlerProvidesAdapter.get(BaseLayerModule$$ModuleAdapter.java:469)
    at com.google.android.apps.common.testing.ui.espresso.ViewInteraction$$InjectAdapter.get(ViewInteraction$$InjectAdapter.java:65)
    at com.google.android.apps.common.testing.ui.espresso.ViewInteraction$$InjectAdapter.get(ViewInteraction$$InjectAdapter.java:20)
    at dagger.ObjectGraph$DaggerObjectGraph.get(ObjectGraph.java:251)
    at com.google.android.apps.common.testing.ui.espresso.Espresso.onView(Espresso.java:58)
    at com.example.espresso.TestExample.testCommand(TestExample.java:28)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at android.test.InstrumentationTestCase.runMethod(InstrumentationTestCase.java:214)
    at android.test.InstrumentationTestCase.runTest(InstrumentationTestCase.java:199)
    at android.test.ActivityInstrumentationTestCase2.runTest(ActivityInstrumentationTestCase2.java:192)
    at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:190)
    at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:175)
    at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:555)
    at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1590)

TestExample.java

public class TestExample extends ActivityInstrumentationTestCase2<ClickActivity>{

    public TestExample() {
        super(ClickActivity.class);
        // TODO Auto-generated constructor stub
    }

    @Override
    protected void setUp() throws Exception {
        // TODO Auto-generated method stub
        super.setUp();
        getActivity();
    }

    public void testCommand(){
        String text = "I Love Espresso!!";
        Espresso.onView(ViewMatchers.withId(R.id.txt)).perform(ViewActions.typeText(text));
        Espresso.onView(ViewMatchers.withId(R.id.button1)).perform(ViewActions.click());
        Espresso.onView(ViewMatchers.withId(R.id.txt)).check(ViewAssertions.matches(ViewMatchers.withText(text)));
    }

}

Manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.espresso"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="9"
        android:targetSdkVersion="17" />

    <instrumentation 
        android:targetPackage="com.example.espresso" 
        android:name="android.test.InstrumentationTestRunner" />


    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
    <uses-library android:name="android.test.runner" />
    <activity android:name = "ClickActivity"></activity>
    </application>


</manifest>

【问题讨论】:

    标签: android android-espresso


    【解决方案1】:

    您的 instrumentation->android:name 值应该是“com.google.android.apps.common.testing.testrunner.GoogleInstrumentationTestRunner”而不是“android.test.InstrumentationTestRunner”。

    然后在配置你的junit测试时一定要选择它:

    如果您需要更多指导,我会在我的博客上发布快速设置。

    【讨论】:

    • 我正在使用 IntelliJ 13,问题是自从我开始使用 Gradle Android 构建工具 0.9.+ 后,IntelliJ 就不再“了解”GoogleInstrumentationTestRunner...有什么想法吗?跨度>
    【解决方案2】:

    澄清@ValeraZakharov 引用的https://code.google.com/p/android-test-kit/wiki/Espresso#Espresso_Setup_Instructions 上的说明

    使用 Android Studio 和 gradle 时,无需更新 AndroidManifest.xml,而是需要编辑文件 build.gradle。将此条目添加到android - defaultConfig 部分:

    android {
        defaultConfig {
            testInstrumentationRunner "com.google.android.apps.common.testing.testrunner.GoogleInstrumentationTestRunner"
        }
    }
    

    另外:右键单击“添加为库”已从 Android Studio 中删除,并替换为:

    android {
        sourceSets {
            // Espresso instrumentation tests
            androidTest.setRoot('src/instrumentTest')
        }
    }
    

    完整部分android 在我的项目中如下所示:

    android {
        compileSdkVersion 17
        buildToolsVersion "19.0.1"
    
        defaultConfig {
            minSdkVersion 15
            targetSdkVersion 16
            versionName getCommitNumber()
            testInstrumentationRunner "com.google.android.apps.common.testing.testrunner.GoogleInstrumentationTestRunner"
        }
    
        sourceSets {
            // Espresso instrumentation tests
            androidTest.setRoot('src/instrumentTest')
        }
    
        packagingOptions {
            // Multiple LICENSE.txt in hamcrest packages.
            exclude 'LICENSE.txt'
        }
    
    }
    

    【讨论】:

      【解决方案3】:

      如果您有一个应用了单元测试的依赖库,则该库应该具有与以下相同的仪器设置。

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

      【讨论】:

        【解决方案4】:

        您的清单中缺少 GoogleInstrumentationTestRunner 条目。之后,确保将测试配置为通过 GITR 运行。请参阅此处的说明:https://code.google.com/p/android-test-kit/wiki/Espresso#Espresso_Setup_Instructions

        【讨论】:

          【解决方案5】:

          正如其他人所提到的,您应该使用 GoogleInstrumentationTestRunner。 如果您使用 ant 构建,您需要在测试项目的 build.xml 中的“项目”指令中添加一个元素,如下所示。

          在 build.xml 中

          <?xml version="1.0" encoding="UTF-8"?>
          <project name="MainActivityTest" default="help">
              ...
          
             <property name="test.runner" value="com.google.android.apps.common.testing.testrunner.GoogleInstrumentationTestRunner" />
          </project>
          

          【讨论】:

            猜你喜欢
            • 2019-08-09
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-04-14
            • 2014-07-31
            • 1970-01-01
            • 2015-04-23
            • 1970-01-01
            相关资源
            最近更新 更多