Kav*_*a_p 5 unit-testing viewmodel android-testing android-mvvm mutablelivedata
我正在为我的LoginViewModel. 我想setValue()在我的MutableLiveData.
为了避免android.os.Looper 中的方法 getMainLooper 未模拟异常,我尝试在test 文件夹Rule内的 ViewModel 文件中添加以下内容。
@Rule
    public InstantTaskExecutorRule mInstantTaskExecutorRule = new InstantTaskExecutorRule();
 
为此,我添加了以下依赖项:
androidTestImplementation 'android.arch.core:core-testing:1.1.1' as dependency.
但是,我仍然无法InstantTaskExecutorRule在我的 LoginViewModelTest 文件中导入。可能是什么问题?
尽管它被导入到androidTest文件夹中,我们在其中编写集成或 UI 测试用例!但不在我们编写 jUnit 测试用例的测试文件夹中!
先感谢您。
请参考构建。梯度文件:
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'
}
小智 0
对于我的情况,我需要添加另一个依赖项testImplementation(我们拥有的是androidTestImplementation):
testImplementation ("android.arch.core:core-testing:1.1.1")
| 归档时间: | 
 | 
| 查看次数: | 4012 次 | 
| 最近记录: |