gpt4 book ai didi

android - 使用 Espresso 时出现 NullPointer 异常

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:49:57 24 4
gpt4 key购买 nike

我正在尝试使用 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)

测试实例.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)));
}

}

list .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>

最佳答案

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

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

如果您需要更多指导,我在我的博客上发布了一个快速设置教程。

关于android - 使用 Espresso 时出现 NullPointer 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19558575/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com