NullPointer Exception when using Espresso

前端 未结 5 996
孤独总比滥情好
孤独总比滥情好 2021-01-19 16:58

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:



        
相关标签:
5条回答
  • 2021-01-19 17:38

    You are missing a GoogleInstrumentationTestRunner entry in your manifest. After that, make sure to configure your tests to run through GITR. See instructions here: https://code.google.com/p/android-test-kit/wiki/Espresso#Espresso_Setup_Instructions

    0 讨论(0)
  • 2021-01-19 17:39

    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: enter image description here

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

    0 讨论(0)
  • 2021-01-19 17:42

    To clarify the instructions on https://code.google.com/p/android-test-kit/wiki/Espresso#Espresso_Setup_Instructions, referenced by @ValeraZakharov

    When using Android Studio and gradle, there is no need to update AndroidManifest.xml, instead the file build.gradle needs to be edited. Add this entry to the section android - defaultConfig:

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

    Also: Right-click "add as a library" has been removed from Android studio, and replaced with:

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

    The complete section android looks like this in my project:

    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 讨论(0)
  • 2021-01-19 17:56

    If you have a dependent library with unit test applied, the libary should have same instrument setup also as below.

    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    
    0 讨论(0)
  • 2021-01-19 18:00

    You should use GoogleInstrumentationTestRunner, as others mentioned. If you build with ant, You need to add an element to the "project" directive in build.xml of the Test Project like below.

    in 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>
    
    0 讨论(0)
提交回复
热议问题