Skip to content Skip to sidebar Skip to footer

Nullpointer Exception When Using Espresso

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 ins

Solution 1:

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.

Solution 2:

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

}

Solution 3:

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"

Solution 4:

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

Solution 5:

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"?><projectname="MainActivityTest"default="help">
    ...

   <propertyname="test.runner"value="com.google.android.apps.common.testing.testrunner.GoogleInstrumentationTestRunner" /></project>

Post a Comment for "Nullpointer Exception When Using Espresso"