15

After coming across this gist : https://gist.github.com/chemouna/00b10369eb1d5b00401b, I noticed it was using the Google Truth library : https://google.github.io/truth/. So I started by following the procedure to add the library in my build.gradle file in Android Studio:

buildscript {
  repositories.mavenLocal()
}

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

But when I wanted to add the static imports for Truth’s entry points for my assertions java class:

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

I got the error that the symbol Truth cannot be resolved. I tried to rebuild my project and implement the solutions stated here: AndroidTestCompile dependencies not recognized in the imports, mainly running the following gradle tasks:

  • ./gradlew app:dependencies
  • assembleAndroidTest

but the problem persists.

Any help on that?

Should I actually add these lines in my build.gradle file ? :

 buildscript {
  repositories.mavenLocal()
}

if I already have these :

repositories {
   mavenCentral()
   jcenter()
   google()
}
Zoe
  • 27,060
  • 21
  • 118
  • 148
ysoa
  • 153
  • 1
  • 5

5 Answers5

15

To use the Java 8 extensions, also include com.google.truth.extensions:truth-java8-extension:0.40.

NOTE

You should call androidTestImplementation

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

Read more information about Truth - Fluent assertions for Java .

IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
  • Thank you for your help @IntelliJ Amiya. I added the compile options and the depending in my build.gradle. But when I sync I got this error : Could not find method compileOptions() for arguments [build_4gfl49u8k92u69bygafbqspv9$_run_closure3@3a4b6224] on project ':app' of type org.gradle.api.Project. – ysoa May 21 '18 at 11:51
  • Should I keep both lines in the dependencies ? : `testImplementation 'com.google.truth:truth:0.40'` and `testImplementation group: 'com.google.truth.extensions', name: 'truth-java8-extension', version: '0.40'` – ysoa May 21 '18 at 11:53
  • @younes-makhtoum Restart IDE and see https://stackoverflow.com/questions/23796404/could-not-find-method-compile-for-arguments-gradle – IntelliJ Amiya May 21 '18 at 11:53
  • @younes-makhtoum you can. no problem – IntelliJ Amiya May 21 '18 at 11:54
  • The problem is still not solved. what pointed you to the fact that I might need to use the Java 8 extensions? From what I understood, it's not mandatory and only required when intending to test specific Java 8 assertions like `Optional` and `Stream`, which is not my case. – ysoa May 21 '18 at 12:04
  • @younes-makhtoum add `androidTestImplementation "com.google.truth:truth::0.40"` – IntelliJ Amiya May 21 '18 at 12:29
  • 1
    Yes! it's fixed now! Thanks a lot. Please update your core response so that I can upvote it and mark it as the right one. – ysoa May 21 '18 at 12:33
3

Add the appropriate dependency to your build file:

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

You need the Java 8 extensions if and only if you're testing Java 8 code (Stream, Optional, etc):

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

Lou Morda
  • 5,078
  • 2
  • 44
  • 49
1

You need to add mavenCentral to your repositories

repositories {
  mavenCentral()
}
dependencies {
  testImplementation "com.google.truth:truth:1.1.2"
}
Thiago
  • 12,778
  • 14
  • 93
  • 110
0

JUST DON'T FORGET TO USE

mavenCentral() instead of jcenter()

allprojects {
   repositories {
      google()
      mavenCentral()
   }
}
ItSNeverLate
  • 533
  • 7
  • 8
0

As android Gradle plugin requires java 11. Please update Gradle JDK enter image description here

Tareq Islam
  • 124
  • 6