google-truth - Android Studio - 未解决的参考:真相
问题描述
我正在尝试在我的项目中包含 Google Truth 框架进行测试。我遵循了有关如何获取项目设置的文档。
这是来自我的应用程序的 build.gradle 文件:
dependencies {
...
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.1.2-alpha01'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.2-alpha01'
androidTestImplementation 'androidx.test.ext:truth:1.1.0'
androidTestImplementation 'com.google.truth:truth:0.43'
}
同步过程成功完成。
然后我尝试运行本地单元测试,例如:
import org.junit.Test
import com.google.common.truth.Truth.*
class CustomExampleUnitTest {
@Test
fun testBlank_isCorrect() {
assertThat("".isBlank()).isTrue()
}
}
我收到 Kotlin 编译器错误:未解决的参考:真相
有几点需要注意:
- 当我尝试通过刚开始键入来使用与 Truth 相关的方法时,对于这些方法中的任何一个都没有任何建议。这无需手动添加导入语句,但是当我从建议的方法中选择合适的方法时,Android Studio 总是自动完成,所以这是我注意到的第一件奇怪的事情。
- 当上面提到的不起作用时,我手动进行了导入,当我输入要导入的内容时,我确实得到了 com.google.common.truth.Truth 的建议......这表明至少 jar 文件在某个地方被发现。手动导入后,Android Studio 开始按照我之前的预期从 Truth 建议方法。
因此,在完成上述步骤后,我尝试运行测试,但仍然遇到未解决的问题。
任何人都可以尝试对此有所了解吗?有没有人遇到过这种情况。我将非常感谢任何形式的帮助!
解决方案
如果您的测试在androidTest
目录中,那么您需要
androidTestImplementation 'com.google.truth:truth:0.43'
但是如果您的测试在test
目录中,那么您需要
testImplementation 'com.google.truth:truth:0.43'
推荐阅读
- wordpress - 如何向 Neve Wordpress 主题添加多种自定义字体?
- r - 如何将矩阵索引代码从 MATLAB 转换为 R
- c# - WPF 在画布上围绕数据绑定元素放置任意内容
- kubernetes - 来自私有注册表 (Github) 的 Helm 部署映像失败
- node.js - 读取外部 JSON 文件 - NodeJs
- string - 搜索字符串的特定部分并将其剪切掉
- python - 使用 statsmodels.api 在 Python 中使用逻辑回归进行训练和测试
- twitter-bootstrap - 如何从自定义颜色计算一致的灰色阴影(灰色 100 到灰色 900)?
- swagger-ui - 如何在 Swagger UI 中使用“-k”参数生成 curl 命令?
- swift - 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“UICollectionView 必须使用非零布局参数初始化”