You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android配置mockwebserver后测试提示Unresolved reference: mockwebserver如何解决

问题解决方案

排查&解决步骤

  • 依赖作用域不匹配
    你当前仅给*本地单元测试(test目录)*添加了mockwebserver依赖:
testImplementation 'com.squareup.okhttp3:mockwebserver3:5.0.0-alpha.2'

如果你的测试代码写在Instrumented测试(androidTest目录,需要跑在真机/模拟器上),该目录下的代码无法访问testImplementation声明的依赖,需要额外添加适配androidTest的依赖:

androidTestImplementation 'com.squareup.okhttp3:mockwebserver3:5.0.0-alpha.2'
  • 版本不匹配冲突
    你项目中okhttp核心依赖版本为4.3.1,但mockwebserver使用的是5.0.0-alpha.2跨大版本的预览版,二者API不兼容会导致引用异常。建议将mockwebserver版本和okhttp版本对齐,修改依赖为:
// 本地单元测试用
testImplementation 'com.squareup.okhttp3:mockwebserver:4.3.1'
// 若需要在androidTest中使用则加下面这行
androidTestImplementation 'com.squareup.okhttp3:mockwebserver:4.3.1'
  • 导入包名修正
    调整依赖后,检查代码中导入语句是否正确,正确导入格式为:
import okhttp3.mockwebserver.MockWebServer
  • 清理IDE缓存
    如果修改后Android Studio仍无提示,执行缓存清理操作:
    点击顶部菜单栏 File > Invalidate Caches...,勾选Clear file system cache and local history后点击Invalidate and Restart,等待IDE重启重新构建项目即可。

内容的提问来源于stack exchange,提问作者petestmart

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.29 05:24:00