Java.lang.IllegalStateException : No instrumentation registered! Must be run under the Registration tool

Java.lang.IllegalStateException : No instrumentation registered! Must be run under the Registration tool … here is a solution to the problem.

Java.lang.IllegalStateException : No instrumentation registered! Must be run under the Registration tool

I’ve been trying to do simple UI tests with Espresso but all my tests fail with the same exception:

java.lang.IllegalStateException: No instrumentation registered! Must run under a registering instrumentation

I’ve found similar questions, but the ones most relevant to me aren’t answered here – I guess it’s because they don’t draw the full picture, so here’s my code. I’ll show only one test because they all fail with the exact same error:

build.gradle(Module: app)

apply plugin: 'com.android.application'

android {
    compileSdkVersion 27
    defaultConfig {
        applicationId "io.github.vinge1718.myrestaurants"
        minSdkVersion 15
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        //testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    testOptions {
        unitTests {
            includeAndroidResources = true
        }
    }
}

dependencies {
    testImplementation "org.robolectric:robolectric:3.8"
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:27.1.1'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation('com.android.support.test.espresso:espresso-core:3.0.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })

    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0'
    androidTestImplementation 'androidx.test:runner:1.1.0'
    androidTestImplementation 'androidx.test:rules:1.1.0'
    androidTestImplementation 'org.hamcrest:hamcrest-library:1.3'
}

build.gradle (project: MyRestaurant)

buildscript {
    
    repositories {
        google()
        jcenter()
        maven { url "https://oss.sonatype.org/content/repositories/snapshots" }
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:3.2.1'
    }
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

The correction here is two tests. I don’t think the bug has anything to do with the test itself, but with the configuration – I agree to correct it

(MainActivityInstrumentationTest.java)

package io.github.vinge1718.myrestaurants;

import android.support.test.runner.AndroidJUnit4;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import androidx.test.rule.ActivityTestRule;

import static androidx.test.espresso.Espresso.onView;
import static androidx.test.espresso.action.ViewActions.click;
import static androidx.test.espresso.action.ViewActions.closeSoftKeyboard;
import static androidx.test.espresso.action.ViewActions.typeText;
import static androidx.test.espresso.assertion.ViewAssertions.matches;
import static androidx.test.espresso.matcher.ViewMatchers.withId;
import static androidx.test.espresso.matcher.ViewMatchers.withText;


@RunWith(AndroidJUnit4.class)
public class MainActivityInstrumentationTest {
    private String mStringToBetyped;

    @Rule
    public ActivityTestRule<MainActivity> mActivityRule = new ActivityTestRule<>(MainActivity.class);

    @Before
    public void initValidString() {
        // Specify a valid string.
        mStringToBetyped = "Portland";
    }

    @Test
    public void validateEditText(){
        onView(withId(R.id.locationEditText))
                .perform(typeText(mStringToBetyped), closeSoftKeyboard())
                .check(matches(withText(mStringToBetyped)));
    }

    @Test
    public void locationIsSentToRestaurantActivity(){
        String location = "Portland";
        onView(withId(R.id.locationEditText)).perform(typeText(location));
        onView(withId(R.id.findRestaurantsButton)).perform(click());
        onView(withId(R.id.locationTextView)).check(matches(withText("Here are all the Restaurants near " + location)));
    }
}

I’ve tried following this Espresso setup documentation here but I keep getting the same error:

Started running tests

java.lang.IllegalStateException: No instrumentation registered! Must
run under a registering instrumentation. at
androidx.test.InstrumentationRegistry.getInstrumentation(InstrumentationRegistry.java:50)
at
androidx.test.InstrumentationRegistry.getTargetContext(InstrumentationRegistry.java:101)
at
androidx.test.rule.ActivityTestRule. (ActivityTestRule.java:144)
at
androidx.test.rule.ActivityTestRule. (ActivityTestRule.java:120)
at
androidx.test.rule.ActivityTestRule. (ActivityTestRule.java:103)
at
io.github.vinge1718.myrestaurants.MainActivityInstrumentationTest. (MainActivityInstrumentationTest.java:25)
at java.lang.reflect.Constructor.newInstance0(Native Method) at
java.lang.reflect.Constructor.newInstance(Constructor.java:334) at
org.junit.runners.BlockJUnit4ClassRunner.createTest(BlockJUnit4ClassRunner.java:217)
at
org.junit.runners.BlockJUnit4ClassRunner$1.runReflectiveCall(BlockJUnit4ClassRunner.java:266)
at
org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at
org.junit.runners.BlockJUnit4ClassRunner.methodBlock(BlockJUnit4ClassRunner.java:263)
at
org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at
org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) at
org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at
org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at
org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at
org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at
org.junit.runners.ParentRunner.run(ParentRunner.java:363) at
android.support.test.runner.AndroidJUnit4.run(AndroidJUnit4.java:101)
at org.junit.runners.Suite.runChild(Suite.java:128) at
org.junit.runners.Suite.runChild(Suite.java:27) at
org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) at
org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at
org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at
org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at
org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at
org.junit.runners.ParentRunner.run(ParentRunner.java:363) at
org.junit.runner.JUnitCore.run(JUnitCore.java:137) at
org.junit.runner.JUnitCore.run(JUnitCore.java:115) at
android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:56)
at
android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:384)
at
android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:2075)

Tests ran to completion.

Here is the test configuration documentation I described in the Espresso setup:

Best Solution

I think this is because the library androidx conflicts with com.android.support.test . If you want to use jetpack, you have to convert all your test libraries to androidx, and if you don’t want that, just delete your androidx libraries and use all com.android.support。测试. Check out my latest answers in Android Instrumentation Testing: No instrumentation registered error. Hope this helps.

Related Problems and Solutions