Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Studio - Unresolved reference: truth

Tags:

google-truth

I am trying to include the Google Truth framework in my project for testing. I followed the documentation on how to get the project setup.

This is from my app's build.gradle file:

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'
}

The syncing process completes successfully.

Then I attempt to run a local unit test for example:

import org.junit.Test
import com.google.common.truth.Truth.*

class CustomExampleUnitTest {

    @Test
    fun testBlank_isCorrect() {
        assertThat("".isBlank()).isTrue()
    }
}

I get a Kotlin compiler error: Unresolved reference: truth

There are a couple of things to note:

  • When I attempt to use a method related to Truth by just starting to type, there is no suggestions for any of those methods. This is without manually adding the import statement, but Android Studio has always done that automatically when I choose the appropriate method from the suggested ones, so this was the first odd things I noticed.
  • When the above mentioned did not work, I manually did the import and as I was typing what to import, I did get suggestions for com.google.common.truth.Truth... which showed me that at least the jar file was somewhere to be found. After this manual import, the Android Studio then started suggesting methods from Truth as I expected before.

So after going through the steps above, I tried to run the test and I still get the unresolved issue.

Could anyone please try to shed some light on this? Has anyone encountered this. I would be really grateful for any kind of help!

like image 446
hermannkuschke Avatar asked Mar 01 '19 07:03

hermannkuschke


2 Answers

If your tests are in the androidTest directory then you need

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

but if your tests are in the test directory then you need

testImplementation 'com.google.truth:truth:0.43'
like image 63
stacksonstacks Avatar answered Dec 25 '22 14:12

stacksonstacks


Blockquote

If your use kotlin use

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

like image 29
jeremex Avatar answered Dec 25 '22 15:12

jeremex