Android Studio 无法解析 Mockito 类

标签 android android-studio kotlin mockito android-testing

尽管 testImplementation 中存在 Mockito 依赖项,但 Android Studio 显示红线提示,它无法解决依赖项

下面是 dependencies task 的输出,配置了 testImplementation

gradle :exo-audio:dependencies --configuration testImplementation



testImplementation - Implementation only dependencies for 'test' sources. (n)
+--- junit:junit:4.12 (n)
+--- org.hamcrest:hamcrest-all:1.3 (n)
+--- org.mockito:mockito-all:1.10.19 (n)
+--- com.squareup.okhttp3:mockwebserver:4.2.1 (n)
\--- com.google.dagger:dagger:2.24 (n)

(n) - Not resolved (configuration is not meant to be resolved)

这是 Android Studio 显示错误的屏幕截图

studio mockito error

I have tried cleaning build cache and invalidate restart, nothing seems to be working. Even I have also tried by removing .idea and .gradle directories manually and loading project again.

I think the problem is with Android Studio, even with studio complaining about the classes, I am able to run tests e.g. for the following test hash code is printed.

@Test
    fun foo() {
        println(repository.hashCode())
    }

最佳答案

终于!我已经在一天结束时解决了它。我将列出我执行的步骤。

  1. 检查了module.iml 文件中的test source directoresclasspath,一切正常

    <sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
    
  2. 已检查本地 gradle 缓存中可用的版本

    allaudin@geek ~/.gradle/caches/modules-2/files-2.1/org.mockito/mockito-core$ ls
    
    1.10.19  2.10.0  2.19.0  2.27.0  2.28.0  2.28.2  2.6.1  2.7.22
    

最后我尝试了不同的版本,它不适用于 2.10.0 版本,然后开始为 2.19.0 版本工作。

I tried to figure out the root cause but I have to code a lot before I go to sleep. Over to experts for figuring out why some versions do not work ;)

关于Android Studio 无法解析 Mockito 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58432673/

相关文章:

java - 使用相机时 Android 中的权限被拒绝

kotlin - 在注解处理阶段获取kotlin fun参数的默认值

Android 微调器在按钮本身上没有波纹,只有它的选项

java - 如何在 Android 中使用后台服务覆盖两次甚至 3 次单击电源按钮甚至音量增大/减小键?

java - 如何限制用户在数据库中多次写入

安卓 : Location Update in Mapview takes to long even tough Location is found

Android 设备监视器无法启动

java - 如何在Android Android 6.0(棉花糖)上创建透明布局?

android - 对Kotlin协程感到困惑

dynamic - Kotlin 中动态/双重调度的限制是什么?