2013-10-24 9 views
4

Я пытаюсь использовать Espresso. Но я получил эту ошибку, и у меня нет, если мне не хватает некоторых кодов.NullPointer Исключение при использовании 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> 

ответ

6

Ваше значение instrumentation-> Android: название должно быть "com.google.android.apps.common.testing.testrunner.GoogleInstrumentationTestRunner" не "android.test.InstrumentationTestRunner".

И тогда не забудьте выбрать его при настройке теста JUnit: enter image description here

Я отправил быстрый TUT установки на моем блоге, если вам нужно больше указаний.

+2

Я использую IntelliJ 13, и проблема заключается в IntelliJ остановил «знать» о 'GoogleInstrumentationTestRunner' с тех пор, как я начал использовать Gradle Android сборки инструментов' 0.9. + '... любые идеи? – JaKXz

3

Для уточнения инструкции по https://code.google.com/p/android-test-kit/wiki/Espresso#Espresso_Setup_Instructions, на которые ссылается @ValeraZakharov

При использовании Android Studio и Gradle, нет необходимости обновлять AndroidManifest.xml, вместо файла build.gradle должен быть отредактирован. Добавить запись в раздел android - defaultConfig:

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

также: Щелкните правой кнопкой мыши «Добавить в библиотеку» была удалена из Android студии, и заменены:

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

} 
0

Вы должны использовать GoogleInstrumentationTestRunner, как упоминалось в других документах. Если вы создаете с помощью ant, вам нужно добавить элемент в директиву «project» в файле 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> 
2

Если у вас есть зависимая библиотека модульного тестирования прикладного, то libary должна иметь одинаковые настройки прибора также, как показано ниже.

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 
Смежные вопросы