使用es preSSO当空指针异常 [英] NullPointer Exception when using Espresso

查看:139
本文介绍了使用es preSSO当空指针异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用长者preSSO。但我得到这个错误,我也没有,如果我失去了一些codeS。

有什么想法就会有强烈的AP preciated。

错误日志:

 显示java.lang.NullPointerException:没有注册仪器仪表。必须在一个登记仪表运行。
    在com.google.android.apps.common.testing.testrunner.util.Checks.checkNotNull(Checks.java:28)
    在com.google.android.apps.common.testing.testrunner.InstrumentationRegistry.getInstance(InstrumentationRegistry.java:23)
    在com.google.android.apps.common.testing.ui.es$p$psso.base.BaseLayerModule.provideTargetContext(BaseLayerModule.java:50)
    在com.google.android.apps.common.testing.ui.es$p$psso.base.BaseLayerModule$$ModuleAdapter$ProvideTargetContextProvidesAdapter.get(BaseLayerModule$$ModuleAdapter.java:101)
    在com.google.android.apps.common.testing.ui.es$p$psso.base.BaseLayerModule$$ModuleAdapter$ProvideTargetContextProvidesAdapter.get(BaseLayerModule$$ModuleAdapter.java:85)
    在com.google.android.apps.common.testing.ui.es$p$psso.base.DefaultFailureHandler$$InjectAdapter.get(DefaultFailureHandler$$InjectAdapter.java:53)
    在com.google.android.apps.common.testing.ui.es$p$psso.base.DefaultFailureHandler$$InjectAdapter.get(DefaultFailureHandler$$InjectAdapter.java:20)
    在com.google.android.apps.common.testing.ui.es$p$psso.base.BaseLayerModule$$ModuleAdapter$ProvideFailureHanderProvidesAdapter.get(BaseLayerModule$$ModuleAdapter.java:555)
    在com.google.android.apps.common.testing.ui.es$p$psso.base.BaseLayerModule$$ModuleAdapter$ProvideFailureHanderProvidesAdapter.get(BaseLayerModule$$ModuleAdapter.java:519)
    在com.google.android.apps.common.testing.ui.es$p$psso.base.BaseLayerModule$FailureHandlerHolder$$InjectAdapter.get(BaseLayerModule$FailureHandlerHolder$$InjectAdapter.java:53)
    在com.google.android.apps.common.testing.ui.es$p$psso.base.BaseLayerModule$FailureHandlerHolder$$InjectAdapter.get(BaseLayerModule$FailureHandlerHolder$$InjectAdapter.java:20)
    在dagger.internal.Linker $ SingletonBinding.get(Linker.java:327)
    在com.google.android.apps.common.testing.ui.es$p$psso.base.BaseLayerModule$$ModuleAdapter$ProvideFailureHandlerProvidesAdapter.get(BaseLayerModule$$ModuleAdapter.java:505)
    在com.google.android.apps.common.testing.ui.es$p$psso.base.BaseLayerModule$$ModuleAdapter$ProvideFailureHandlerProvidesAdapter.get(BaseLayerModule$$ModuleAdapter.java:469)
    在com.google.android.apps.common.testing.ui.es$p$psso.ViewInteraction$$InjectAdapter.get(ViewInteraction$$InjectAdapter.java:65)
    在com.google.android.apps.common.testing.ui.es$p$psso.ViewInteraction$$InjectAdapter.get(ViewInteraction$$InjectAdapter.java:20)
    在dagger.ObjectGraph $ DaggerObjectGraph.get(ObjectGraph.java:251)
    在com.google.android.apps.common.testing.ui.es presso.Es presso.onView(ES presso.java:58)
    在com.example.es presso.TestExample.testCommand(TestExample.java:28)
    在java.lang.reflect.Method.invokeNative(本机方法)
    在android.test.InstrumentationTestCase.runMethod(InstrumentationTestCase.java:214)
    在android.test.InstrumentationTestCase.runTest(InstrumentationTestCase.java:199)
    在android.test.ActivityInstrumentationTestCase2.runTest(ActivityInstrumentationTestCase2.java:192)
    在android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:190)
    在android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:175)
    在android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:555)
    在android.app.Instrumentation $ InstrumentationThread.run(Instrumentation.java:1590)
 

TestExample.java

 公共类TestExample扩展ActivityInstrumentationTestCase2< ClickActivity> {

    公共TestExample(){
        超(ClickActivity.class);
        // TODO自动生成构造函数存根
    }

    @覆盖
    保护无效设置()抛出异常{
        // TODO自动生成方法存根
        super.setUp();
        getActivity();
    }

    公共无效testCommand(){
        字符串文本=我爱居preSSO!;
        居presso.onView(ViewMatchers.withId(R.id.txt))执行(ViewActions.typeText(文本))。
        居presso.onView(ViewMatchers.withId(R.id.button1))执行(ViewActions.click())。
        Es$p$psso.onView(ViewMatchers.withId(R.id.txt)).check(ViewAssertions.matches(ViewMatchers.withText(text)));
    }

}
 

的Manifest.xml

 < XML版本=1.0编码=UTF-8&GT?;
<舱单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    包=com.example.es preSSO
    安卓版code =1
    机器人:VERSIONNAME =1.0>

    <用途-SDK
        安卓的minSdkVersion =9
        机器人:targetSdkVersion =17/>

    <仪器仪表
        机器人:targetPackage =com.example.es preSSO
        机器人:名称=android.test.InstrumentationTestRunner/>


    <应用
        机器人:allowBackup =真
        机器人:图标=@可绘制/ ic_launcher
        机器人:标签=@字符串/ APP_NAME
        机器人:主题=@风格/ AppTheme>
    <使用库机器人:名称=android.test.runner/>
    <活动机器人:名称=ClickActivity>< /活性GT;
    < /用途>


< /舱单>
 

解决方案

您的instrumentation-> Android的值:名称应为com.google.android.apps.common.testing.testrunner.GoogleInstrumentationTestRunner而不是android.test .InstrumentationTestRunner。

,然后一定配置JUnit测试时选择它:

我贴在我的博客快速设置啧啧,如果你需要更多的指导。

I'm trying to use Espresso. But I got this error and I have no if I'm missing some codes.

any thoughts will be highly appreciated.

error log:

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>

解决方案

Your value of instrumentation->android:name should be "com.google.android.apps.common.testing.testrunner.GoogleInstrumentationTestRunner" not "android.test.InstrumentationTestRunner".

And then be sure to select it when configuring your junit test:

I posted a quick setup tut on my blog if you need more guidance.

这篇关于使用es preSSO当空指针异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆