14

遇到这个要点后:https ://gist.github.com/chemouna/00b10369eb1d5b00401b ,我注意到它正在使用Google Truth库:https ://google.github.io/truth/ 。因此,我首先按照以下过程build.gradle在 Android Studio 中的文件中添加库:

buildscript {
  repositories.mavenLocal()
}

dependencies {
  testImplementation "com.google.truth:truth:0.40"
}

但是当我想为我的断言 java 类添加 Truth 入口点的静态导入时:

import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth.assertWithMessage;

我收到符号Truth无法解析的错误。我尝试重建我的项目并实施此处所述的解决方案:AndroidTestCompile dependencies not recognize in the imports,主要运行以下 gradle 任务:

  • ./gradlew 应用程序:依赖项
  • 组装AndroidTest

但问题仍然存在。

有什么帮助吗?

我真的应该在我的 build.gradle 文件中添加这些行吗?:

 buildscript {
  repositories.mavenLocal()
}

如果我已经有了这些:

repositories {
   mavenCentral()
   jcenter()
   google()
}
4

5 回答 5

12

要使用 Java 8 扩展,还包括 com.google.truth.extensions:truth-java8-extension:0.40。

笔记

你应该打电话androidTestImplementation

androidTestImplementation "com.google.truth:truth::0.40" // Latest version 1.1.3

阅读有关Java 的 Truth-Fluent 断言的更多信息。

于 2018-05-21T11:43:12.097 回答
3

将适当的依赖项添加到您的构建文件中:

testImplementation "com.google.truth:truth:1.0.1"

当且仅当您正在测试 Java 8 代码(Stream、Optional 等)时,您才需要 Java 8 扩展:

testImplementation "com.google.truth.extensions:truth-java8-extension:1.0.1"

于 2020-08-02T20:17:31.480 回答
1

您需要将 mavenCentral 添加到您的存储库

repositories {
  mavenCentral()
}
dependencies {
  testImplementation "com.google.truth:truth:1.1.2"
}
于 2021-02-17T13:06:08.397 回答
0

由于 android Gradle 插件需要 java 11。请更新 Gradle JDK 在此处输入图像描述

于 2022-02-08T07:58:29.727 回答
0

只是不要忘记使用

mavenCentral() 而不是 jcenter()

allprojects {
   repositories {
      google()
      mavenCentral()
   }
}
于 2021-06-28T03:53:43.293 回答