Question

Didn't find class "android.support.test.runner.AndroidJUnitRunner"

I'm trying to use an instrumented test and I updated my project to AndroidX. But I get the following error while trying to run:

2018-10-09 14:22:29.468 13263-13263/xxx.yyy E/AndroidRuntime: FATAL EXCEPTION: main
    Process: xxx.yyy, PID: 13263
    java.lang.RuntimeException: Unable to instantiate instrumentation ComponentInfo{xxx.yyy.test/android.support.test.runner.AndroidJUnitRunner}: java.lang.ClassNotFoundException: Didn't find class "android.support.test.runner.AndroidJUnitRunner" on path: DexPathList[[],nativeLibraryDirectories=[/system/lib, /vendor/lib]]
        at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5730)
        at android.app.ActivityThread.-wrap1(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1661)
        at android.os.Handler.dispatchMessage(Handler.java:105)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6541)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
     Caused by: java.lang.ClassNotFoundException: Didn't find class "android.support.test.runner.AndroidJUnitRunner" on path: DexPathList[[],nativeLibraryDirectories=[/system/lib, /vendor/lib]]
        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:93)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
        at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5728)
        at android.app.ActivityThread.-wrap1(Unknown Source:0) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1661) 
        at android.os.Handler.dispatchMessage(Handler.java:105) 
        at android.os.Looper.loop(Looper.java:164) 
        at android.app.ActivityThread.main(ActivityThread.java:6541) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) 

My test looks like this:

package xxx.yyy;

import android.content.Context;
import androidx.test.InstrumentationRegistry;
import androidx.test.runner.AndroidJUnit4;

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

import static org.junit.Assert.*;

/**
 * Instrumented test, which will execute on an Android device.
 *
 * @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
 */
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
    @Test
    public void useAppContext() {
        // Context of the app under test.
        Context appContext = InstrumentationRegistry.getTargetContext();

        assertEquals("xxx.yyy", appContext.getPackageName());
    }
}
 46  16573  46
1 Jan 1970

Solution

 71

Inside the build.gradle file there you find the defaultConfig section. With the testInstrumentationRunner attribute the respective test runner can be specified. There the test runner from the android.support package might be specified:

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

this has to be change to the test runner from the androidx package:

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
2018-11-13

Solution

 34

I had the same issue when i forgot to add a dependency to the android test runner.
So adding this helped:

androidTestImplementation("androidx.test:runner:1.2.0")
2019-11-22