'Android Studio - ActivityInstrumentationTestCase2 not found?

I am trying to unit test my MainActivty. My project cannot find ActivityInstrumentationTestCase2 and android.test package. I changed the target SDK to 27 to see if that resolves that problem but to no avail. This is how my gradle looks like:

plugins {
    id 'com.android.application'
}

android {
    compileSdk 32

    defaultConfig {
        applicationId "aziz.ai.gpsspeedometer"
        minSdk 21
        targetSdk 32
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    sourceSets {
        main {
            java {
                srcDirs 'src\\main\\java', 'src\\main\\java\\common'
            }
        }
    }
}

dependencies {
    implementation 'androidx.appcompat:appcompat:1.3.0'
    implementation 'com.google.android.material:material:1.4.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
    implementation 'com.google.android.gms:play-services-location:19.0.1'
}


Solution 1:[1]

ActivityInstrumentationTestCase2 is deprecated on API level 24 put compileSdkVersion 23 or upgrade the test class to the new test classes

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Taleb Souli