Android Studio - Unresolved reference: truth
Asked Answered
L

3

8

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!

Labor answered 1/3, 2019 at 7:29 Comment(0)
G
26

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'
Gleeman answered 18/3, 2019 at 16:32 Comment(0)
C
4

Blockquote

If your use kotlin use

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

Choreography answered 13/5, 2021 at 14:9 Comment(1)
still had to add import com.google.common.truth.Truth.* in addition to that. No auto suggestions otherwise. Find latest truth extension version on developer.android.com/jetpack/androidx/releases/testPleuron
A
1

In my case, the Truth library is not auto-suggested from Android Studio and IDEA (I still don't know why). When typing Truth, it lists out like this auto correct with Truth

or when typing import com.google., common sub-package name doesn't even show auto correct with import com.google.

However, when I run by typing full import, it works like a miracle.

import com.google.common.truth.Truth

after force import truth

(it takes me a morning to figure out this mysterious)

Absorbefacient answered 21/3, 2021 at 3:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.