5

エスプレッソを使ってみました。しかし、このエラーが発生しました。いくつかのコードが欠落している場合はありません。

どんな考えでも大歓迎です。

エラーログ:

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

}

マニフェスト.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>
4

5 に答える 5

3

@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'
    }

}
于 2014-05-09T15:19:47.787 に答える
2

単体テストが適用された依存ライブラリがある場合、ライブラリには以下と同じ機器設定が必要です。

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
于 2015-03-04T10:43:46.453 に答える
0

他の人が述べたように、GoogleInstrumentationTestRunner を使用する必要があります。ant でビルドする場合は、以下のようにテスト プロジェクトの build.xml の「project」ディレクティブに要素を追加する必要があります。

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>
于 2014-05-13T14:49:53.930 に答える
0

マニフェストに GoogleInstrumentationTestRunner エントリがありません。その後、テストを GITR 経由で実行するように構成してください。こちらの手順を参照してください: https://code.google.com/p/android-test-kit/wiki/Espresso#Espresso_Setup_Instructions

于 2013-10-25T04:38:58.183 に答える