问题
I am writing test cases for my LoginViewModel
. Where I want to perform setValue()
operation on my MutableLiveData
.
To avoid Method getMainLooper in android.os.Looper not mocked Exception I am trying to add the following Rule
in my ViewModel file inside test folder.
@Rule
public InstantTaskExecutorRule mInstantTaskExecutorRule = new InstantTaskExecutorRule();
for that, I have added the following dependency:
androidTestImplementation 'android.arch.core:core-testing:1.1.1' as dependency.
But still, I am not able to import the InstantTaskExecutorRule
in my LoginViewModelTest file. What could be the issue?
Though it's getting imported in androidTest folder where we write integration or UI test cases! But not in test folder where we write jUnit test cases!
Thank you in advance.
Please refer the build. gradle file:
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.test.login"
minSdkVersion 19
targetSdkVersion 28
versionCode 2
versionName "1.1"
multiDexEnabled true
testInstrumentationRunner
"android.support.test.runner.AndroidJUnitRunner"
}
dataBinding {
enabled = true
}
testOptions {
unitTests.returnDefaultValues = true
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
def design = "28.0.0"
implementation "com.android.support:appcompat-v7:$design"
implementation "com.android.support:design:$design"
implementation "com.android.support:recyclerview-v7:$design"
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'com.android.support:support-v4:28.0.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
androidTestImplementation 'android.arch.core:core-testing:1.1.1'
implementation 'io.reactivex.rxjava2:rxjava:2.1.9'
implementation 'io.reactivex.rxjava2:rxandroid:2.0.2'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test:rules:1.0.2'
androidTestImplementation 'com.squareup.retrofit2:retrofit-mock:2.0.0'
implementation 'com.squareup.okhttp3:mockwebserver:3.8.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-intents:3.0.2'
implementation 'com.jakewharton:butterknife:8.8.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
def daggerVer = 2.16
implementation "com.google.dagger:dagger:$daggerVer"
annotationProcessor "com.google.dagger:dagger-compiler:$daggerVer"
compileOnly 'javax.annotation:jsr250-api:1.0'
implementation 'javax.inject:javax.inject:1'
implementation 'com.squareup.retrofit2:retrofit:2.4.0'
implementation 'com.squareup.okhttp3:logging-interceptor:3.9.1'
implementation 'com.squareup.retrofit2:converter-gson:2.3.0'
//implementation 'com.google.android.material:material:1.0.0'
def lifeCycle = "1.1.1"
implementation "android.arch.lifecycle:extensions:$lifeCycle"
/*implementation "android.arch.lifecycle:runtime:$lifeCycle"
annotationProcessor "android.arch.lifecycle:compiler:$lifeCycle"*/
implementation "android.arch.persistence.room:runtime:$lifeCycle"
implementation 'com.mikhaellopez:circularimageview:3.2.0'
annotationProcessor
"android.arch.persistence.room:compiler:$lifeCycle"
implementation 'com.squareup.picasso:picasso:2.71828'
implementation 'com.android.support:multidex:1.0.3'
implementation 'commons-beanutils:commons-beanutils:1.9.3'
implementation 'org.mockito:mockito-core:2.23.4'
androidTestImplementation 'org.mockito:mockito-android:2.18.3'
}
来源:https://stackoverflow.com/questions/56029125/not-able-to-import-instanttaskexecutorrule-in-my-junit-test-case-even-after-ad