Android Unit Test Androidx Migration (Kotlin)

December 28, 2018
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:

This work is licensed under a
Creative Commons Attribution-NonCommercial 4.0 International License.