Unresolved reference: Matchers
Asked Answered
T

2

6

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.

Treblinka answered 6/10, 2018 at 8:22 Comment(0)
I
3

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.

Intelligentsia answered 29/8, 2020 at 11:3 Comment(0)
M
2

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.

Merrygoround answered 24/10, 2018 at 18:26 Comment(1)
I have androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1' but the error still existVaginal

© 2022 - 2024 — McMap. All rights reserved.