Home > Software engineering >  UI Testing Error: No instrumentation registered! Must run under a registering instrumentation
UI Testing Error: No instrumentation registered! Must run under a registering instrumentation

Time:01-20

I started learning Espresso
I need to start the activity and set the permission for the camera.

@RunWith(AndroidJUnit4ClassRunner::class)
class MainActivityTest{

    @get:Rule
    var activityScenarioRule = activityScenarioRule<MainActivity>()

    @get:Rule
    val permissionRule = GrantPermissionRule.grant(
        Manifest.permission.CAMERA
    )

    @Test
    fun checkName(){
        onView(withId(R.id.etName)).perform(typeText("Alex"))
        onView(withId(R.id.btSend)).perform(click())
        onView(withId(R.id.tvName)).check(matches(isDisplayed()))
        onView(withId(R.id.tvName)).check(matches(withText("Alex")))
    }
}

But after running the test, returns an error

java.lang.IllegalStateException: No instrumentation registered! Must run under a registering instrumentation.
at android.support.test.InstrumentationRegistry.getInstrumentation(InstrumentationRegistry.java:44)
at android.support.test.InstrumentationRegistry.getTargetContext(InstrumentationRegistry.java:82)
at android.support.test.runner.permission.PermissionRequester.<init>(PermissionRequester.java:66)
at android.support.test.rule.GrantPermissionRule.<init>(GrantPermissionRule.java:69)
at android.support.test.rule.GrantPermissionRule.grant(GrantPermissionRule.java:88)
at com.example.espressotest.MainActivityTest.<init>(MainActivityTest.kt:26)

My dependencies

dependencies {

    implementation 'androidx.core:core-ktx:1.7.0'
    implementation 'androidx.appcompat:appcompat:1.5.1'
    implementation 'com.google.android.material:material:1.7.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.5'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'

    androidTestImplementation 'com.android.support.test:rules:1.0.2'

    androidTestImplementation "androidx.test:runner:1.5.2"
    androidTestImplementation "androidx.test:runner:1.5.2"
    androidTestImplementation "androidx.test.ext:junit-ktx:1.1.5"
}

I expect that when the test starts, the camera permission will be automatically applied

CodePudding user response:

Are you sure that your test classes are inside app/src/androidTest path?

If you put them inside the app/src/test, this error will appear, because only Unit tests go inside the mentioned path.

CodePudding user response:

You need to set an instrumentation runner since espresso test is an instrumented test :

android {
    defaultConfig {
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
}

https://developer.android.com/training/testing/espresso/setup#set-instrumentation-runner

  • Related