我正在为我的
LoginViewModel
编写测试用例。我想在我的 setValue()
上执行 MutableLiveData
操作。
为了避免 android.os.Looper 中的方法 getMainLooper 未被嘲笑异常,我尝试在
test 文件夹中的 ViewModel 文件中添加以下
Rule
。
@Rule
public InstantTaskExecutorRule mInstantTaskExecutorRule = new InstantTaskExecutorRule();
androidTestImplementation 'android.arch.core:core-testing:1.1.1' as dependency.
但是,我仍然无法在 LoginViewModelTest 文件中导入
InstantTaskExecutorRule
。可能是什么问题?
尽管它被导入到我们编写集成或 UI 测试用例的 androidTest 文件夹中!但不在我们编写 jUnit 测试用例的 test 文件夹中!
提前谢谢您。
请参考构建。 gradle文件:
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'
}
testImplementation
添加另一个依赖项(我们拥有的是
androidTestImplementation
):
testImplementation ("android.arch.core:core-testing:1.1.1")