Android IllegalStateException No instrumentation registered! Must run under a registering instrumentation

The problem is with these gradle lines:

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
testInstrumentationRunner "com.android.test.runner.MultiDexTestRunner"

You need a MultiDexTestRunner, but this class is derived from InstrumentationTestRunner. Your code needs a test runner that is derived from AndroidJUnitRunner

A solution is to create a custom TestRunner that extends AndroidJUnitRunner and implements the MultiDex code:

import android.os.Bundle;
import android.support.multidex.MultiDex;
import android.support.test.runner.AndroidJUnitRunner;

public class TestRunner extends AndroidJUnitRunner
{
    @Override
    public void onCreate(Bundle arguments)
    {
        MultiDex.install(getTargetContext());
        super.onCreate(arguments);
    }
}

You only need to add a single Gradle line that references your custom testInstrumentationRunner. This will ensure that the build system generates the AndroidManifest.xml properties that are needed to run the tests with this runner.

I had similar error and was struggling a lot to fix it. Although the accepted answer helped the OP, it did not solve my case, so I decided it is good if I record my solution here, hopefully it will help somebody else too.

My problem was that I was mixing AndroidJUnit4, InstrumentationRegistry, ApplicationProvider and AndroidJUnitRunnerversions / packages. Make sure they all are of the same generation. These are the classes that made it all run for me:

  • androidx.test.runner.AndroidJUnitRunner
  • androidx.test.platform.app.InstrumentationRegistry
  • androidx.test.ext.junit.runners.AndroidJUnit4
  • androidx.test.core.app.ApplicationProvider

for these I needed the following in the dependencies part of my build.gradle

androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
androidTestImplementation 'androidx.test:core:1.2.0'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test:rules:1.2.0'

And of course the correct

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

in my defaultConfig of the build.gradle