Unresolved reference: Matchers - android

I am unable to run the unit test when I import org.hamcrest.Matchers as I need lessThan(). My instrumentation tests compile properly while using the greaterThan matcher but not the unit tests
Code:
import org.hamcrest.CoreMatchers.*
import org.hamcrest.Matchers.lessThan
import org.junit.Assert.assertThat
import org.junit.Test
import java.util.*
Gradle Logs: https://pastebin.com/ibgzzrg1
Removing the 2nd line makes the project compile and runs the test.

In my case, I received a compiler error "Unresolved reference: Matchers" while running the Task :compileTestKotlin. It turned out that I had declared the hamcrest dependency as testRuntimeOnly. Changing it to testImplementation fixed the issue.
testImplementation("org.hamcrest:hamcrest:2.2")
It is interesting to check out the documentation here.

I had this same issue. What I was finding was that I was only experiencing this issue with instrumented tests. Non-instrumented tests were able to import and run fine.
What fixed this for me was adding androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' to my dependencies.
I had removed this thinking it wasn't necessary, since none of my tests explicitly use Espresso. Turns out it is, for reasons beyond my understanding.

Related

jakewharton Espresso Idling Resource won't import

I'm writing tests using okhttp3, mockwebserver, and retrofit2 in Android/Kotlin.
Everything I look up tells me to use the jakewharton idling resource, but it will not import into my project.
I got the info straight from Jake Wharton's github
Screenshot of my build.gradle
Is there anything I can do to fix this?
Are there decent alternatives to adding this type of idle resource to my test suite?
It looks like the build.gradle dependencies no longer support androidTestCompile.
The dependency should switch from:
androidTestCompile 'com.jakewharton.espresso:okhttp3-idling-resource:1.0.0'
to
androidTestImplementation 'com.jakewharton.espresso:okhttp3-idling-resource:1.0.0'

In a Kotlin unit test, why does Android Studio show Mockito as unresolved reference EVEN THOUGH it compiles & tests pass?

Pretty weird problem: Mockito deps look broken even though they actually work.
Why does this happen and how to fix it?
The tooltip over those errors says "unresolved reference".
Some details:
These are unit tests (under app/src/test), not Android instrumentation tests.
This only happens when the test is Kotlin. In equivalent Java test, Mockito deps show up just fine in Android Studio.
To reiterate, even though Mockito stuff is shown in red, it still works: the test compiles and passes, both on the command line and in Android Studio.
In build.gradle, under dependencies:
testImplementation 'junit:junit:4.12'
testImplementation 'org.mockito:mockito-core:2.19.0'
// ...
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.3.50"
(Also I have a file named org.mockito.plugins.MockMaker containing mock-maker-inline, so that Mockito works with final Kotlin classes.)
Edit: This shouldn't be relevant, but for Android instrumentation tests, there's also this. (I had some problems updating to Mockito 2 earlier, so sticking with 1.10.19 there.)
// Here keeping older Mockito for now
androidTestImplementation 'org.mockito:mockito-core:1.10.19'
// dexmaker needed for Mockito to work in androidTest
androidTestImplementation 'com.google.dexmaker:dexmaker:1.2'
androidTestImplementation 'com.google.dexmaker:dexmaker-mockito:1.2'
Using Android Studio 3.5.3
What I've tried
Re-sync the project (build.gradle)
Run clean Gradle task inside AS
Close the project and open it again
Quit Android Studio completely and open it again
Update
I think this is related to several versions of Mockito being present in the project.
When I Cmd-click on the names shown in red, it takes me to sources in mockito-core-1.9.5-sources.jar; but when I Cmd-click on ArgumentMatchers or MockitoJUnitRunner, it takes me to mockito-core-2.19.0-sources.jar, the correct one.
I investigated with app:dependencies, and the only reference to Mockito 1.9.5 is through dexmaker-mockito.
+--- com.google.dexmaker:dexmaker-mockito:1.2
| +--- com.google.dexmaker:dexmaker:1.2
| \--- org.mockito:mockito-core:1.9.5 -> 1.10.19 (*)
But as dexmaker dependency is only for androidTest it shouldn't affect anything under test, right...? 🤔
Edit: indeed, the androidTest deps somehow confused Android Studio; commenting out all mockito and dexmaker deps in androidTestImplementation removed the faulty error highlights (but as mentioned, the different version was used for a reason).
In the end, Android Studio update fixed this.
Oh well, this was a bug in Android Studio: updating to latest version (3.6.2) fixed it.
The androidTestImplementation dependencies were conflicting with testImplementation ones; see updated question for details.

Android Studio doesn't import androidx testing classes properly

I have been working on sample UIAutomator project. I have created new Testcases using AndroidX Testing libraries.
I am able to build, run the test cases from the command line. But when I tried to open the app from Android Studio I can see most of the classes are not imported properly. For eg.,
import androidx.test.uiautomator.UiObject2;
import org.junit.Before;
The above two imports are showing as not imported. Likewise lots of classes are showing the same error except android.content.Context, android.content.Intent, etc.,
Can someone through some light on this. I am able to execute the testcases properly from the command line but not able to execute them properly from Android Studio.
I have Restarted PC/Studio, cleared caches, still the problem exists.
below is the dependency I have added in app/build.gradle.
androidTestImplementation 'androidx.test.uiautomator:uiautomator:2.2.0'
androidTestImplementation 'androidx.test:core:1.0.0'
androidTestImplementation 'androidx.test.ext:junit:1.0.0'
androidTestImplementation 'androidx.test:runner:1.1.0'
androidTestImplementation 'junit:junit:4.12'
Added the below code in gradle.properties
android.useAndroidX=true
android.enableJetifier=true
I have pretty much followed the same what I have got from android-testing-master/ui/uiautomator sample code, not sure what I am missing.
I faced same problem too (Android Studio 3.4.2), uiautomator was red:
import androidx.test.uiautomator.UiDevice
"Clean Project" and "Rebuild Project" didn't help, even invalidating caches. But choosing another Build Variant helped me.

import fizzbuzz not found

I have 'inherited' a source code that I should maintain. It contains import statements about fizzbuzz and dagger that fail. I never used those and am stuck now.
Here the dependencies in build.gradle:

Import statement for dagger cannot be resolved

I am 'lucky' to have been charged with maintaining a program developed by someone else, and I come across the following problem with an import statement:
import com.fizzbuzz.android.dagger.InjectingDialogFragment;
which Android Studio cannot resolve. I am totally new to dagger...
I have the following in build.gradle dependencies:
compile 'com.fizz-buzz:fb-android-dagger:1.0.1'
compile 'com.fizz-buzz:fb-android-bluetooth:1.0.3'
InjectingDialogFragment is not available in the version you are currently using. Please use compile 'com.fizz-buzz:fb-android-dagger:1.0.3' instead.
For better understating on how Dagger works you can use this blog http://antonioleiva.com/dagger-android-part-2/

Categories

Resources