Android Unit Test Androidx Migration (Kotlin)

Cannot resolve symbol 'AndroidJUnit4'

Change module build.gradle.

android {

    ...

    defaultConfig {

        ...

        // testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
}

dependencies {
    testImplementation 'junit:junit:4.12'
    // androidTestImplementation 'com.android.support.test:runner:1.0.2'
    // androidTestImplementation 'androidx.test:runner:1.1.1'
    androidTestImplementation "androidx.test.ext:junit:1.1.0"
    androidTestImplementation "androidx.test.ext:junit-ktx:1.1.0"
    // androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    androidTestImplementation 'androidx.test.espresso:espresso-intents:3.1.1'

    ...
}    

AndroidJUnit4.class is deprecated

androidx.test.runner.AndroidJUnit4 is deprecated. use androidx.test.ext.junit.runners.AndroidJUnit4 instead.

In module build.gradle, make sure to replace com.android.support.test:runner:1.0.2 or androidx.test:runner:1.1.1 with androidx.test.ext:junit:1.1.0.

Test running failed: Instrumentation run failed due to 'java.lang.ClassNotFoundException'

Make sure to include androidTestImplementation 'androidx.test.espresso:espresso-intents:3.1.1'.

References:

❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.