我正在使用“ Android Jetpack WorkManager”开发一个Android应用程序。

我的演示者代码使用它。

我想测试我的演示者代码。

首先,我尝试使用JUnit测试,但是由于WorkManager的缘故,我将其更改为AndroidTest。
以下是我的build.gradle。

dependencies {
    testImplementation 'junit:junit:4.12'
    testImplementation 'org.mockito:mockito-all:1.10.19'
}


在这种情况下,我无法在AndroidTest代码中使用“模拟”。
因此,我将依赖项更改为:

dependencies {
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'org.mockito:mockito-all:1.10.19'
}


然后,我无法在我的JUnit测试代码中使用“模拟”。
所以我尝试了:

dependencies {
    testImplementation 'junit:junit:4.12'
    testImplementation 'org.mockito:mockito-all:1.10.19'
    androidTestImplementation 'org.mockito:mockito-all:1.10.19'
}


这也行不通...

我应该怎么做?

最佳答案

如果您需要模拟android组件,则可以使用Robolectric在单元测试中进行。

关于android - Android:如何在两个测试代码上都使用Mock?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55486497/

10-15 11:18