我想设置我的项目进行单元测试。
我尝试按照 Android's page 上的说明进行操作:
// Unit testing dependencies
testCompile 'junit:junit:4.12'
// Set this dependency if you want to use Mockito
testCompile 'org.mockito:mockito-core:1.10.19'
// Set this dependency if you want to use Hamcrest matching
androidTestCompile 'org.hamcrest:hamcrest-library:1.1'
但是,当这样做并创建测试时,我得到:
"Cannot resolve symbol 'junit'"
"Cannot resolve symbol 'mockito'"
在 Vogel's tutorial ,需要更多的依赖项,而我想要最少的依赖项。
此外,使用 Vogel 的教程,我得到:
Warning:Conflict with dependency 'com.android.support:support-annotations'. Resolved versions for app (22.2.1) and test app (22.2.0) differ.
所以我的问题是:如何让 Android 页面的依赖项发挥作用?
最佳答案
支持注释问题是一个已知问题。您可以找到信息in their issue tracker 。要解决此问题,请在主(应用程序,而不是模块)build.gradle 文件中的 allprojects 部分添加
configurations.all {
resolutionStrategy.force 'com.android.support:support-annotations:22.2.0'
}
(请参阅同一链接中的答案 #11)。
关于额外的依赖项,您将需要 dexmaker 和 dexmaker-mockito 来使用mockito在设备/模拟器中进行测试,因为它们在需要 .dex 文件的 Dalvik VM 上运行,而mockito 生成 .class 文件。单元测试就像添加到 Android Studio 的最新单元测试一样,在本地 JVM 中运行,因此它可能应该在没有 dexmaker 的情况下运行,但我目前还无法确认这一点。
关于android - 在Android Studio中设置junit和mockito时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31848654/