I followed Android Developer doc and try to make a simple unit test in a Android Project.
But I try to run the unit test with ApplicationProvider.getApplicationContext<Context>()
than I got a exception:
java.lang.IllegalStateException: No instrumentation registered! Must run under a registering instrumentation.
It seen like I do not registered instrumentation. but the doc dose not mention any thing about this.
It works normal if I do implement Robolectric Library and use @RunWith(Robolectrictestrunner::class.java)
(and also I need to endure long time setting at first run.). But it's pretty strange because the doc says include the Robolectric environment in androidx.test:core
I want to know did missing something to setting or other right way to unit test with something like SparseArray or the other thing belong Android SDK.
Here is my build.gradle:
plugins {
id 'com.android.application'
id 'kotlin-android'
}
android {
compileSdkVersion 30
buildToolsVersion "30.0.2"
defaultConfig {
applicationId "com.example.myapplication2"
minSdkVersion 23
targetSdkVersion 30
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
}
kotlinOptions {
jvmTarget = '1.8'
}
testOptions {
unitTests.includeAndroidResources = true
}
}
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.3.2'
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.2.1'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
// Required -- JUnit 4 framework
testImplementation 'junit:junit:4.13.1'
// Optional -- Robolectric environment
testImplementation 'androidx.test:core:1.3.0'
// Optional -- Mockito framework
testImplementation 'org.mockito:mockito-core:2.19.1'
testImplementation 'org.robolectric:robolectric:4.4'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}