7

我正在尝试在我的项目中包含 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 建议方法。

因此,在完成上述步骤后,我尝试运行测试,但仍然遇到未解决的问题。

任何人都可以尝试对此有所了解吗?有没有人遇到过这种情况。我将非常感谢任何形式的帮助!

4

3 回答 3

23

如果您的测试在androidTest目录中,那么您需要

androidTestImplementation 'com.google.truth:truth:0.43'

但是如果您的测试在test目录中,那么您需要

testImplementation 'com.google.truth:truth:0.43'
于 2019-03-18T16:32:41.063 回答
3

块引用

如果你使用 kotlin 使用

testImplementation 'androidx.test.ext:truth:1.3.0'

于 2021-05-13T14:09:31.423 回答
0

就我而言,Android Studio 和 IDEA 不会自动建议 Truth 库(我仍然不知道为什么)。键入时Truth,它会像这样列出 自动更正真相

或输入时import com.google.common子包名称甚至不显示 使用 import com.google 自动更正。

但是,当我通过输入完全导入运行时,它就像一个奇迹。

import com.google.common.truth.Truth

强制导入真相后

(我花了一个上午才弄清楚这个神秘)

于 2021-03-21T03:57:35.873 回答