未解决的引用:Matchers

6

当我导入 org.hamcrest.Matchers 并需要使用 lessThan() 时,我无法运行单元测试。在使用 greaterThan 匹配器时,我的仪表板测试可以正常编译,但单元测试却不行。

代码:

import org.hamcrest.CoreMatchers.*
import org.hamcrest.Matchers.lessThan
import org.junit.Assert.assertThat
import org.junit.Test
import java.util.*

Gradle日志:https://pastebin.com/ibgzzrg1

删除第二行可以使项目编译并运行测试。

2个回答

3
在我的情况下,当我运行Task: compileTestKotlin时,我收到了一个编译器错误"Unresolved reference: Matchers"。后来发现我将hamcrest依赖声明为“testRuntimeOnly”。将其更改为“testImplementation”解决了这个问题。
testImplementation("org.hamcrest:hamcrest:2.2")

在这里查看文档here是非常有趣的。


2

我曾经遇到过同样的问题。我发现只有在 仪器化 测试中才会出现这个问题,非仪器化测试可以正常导入和运行。

解决方法是在我的依赖项中添加 androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'

我曾经移除了它,认为它不是必要的,因为我的测试中没有一个显式使用 Espresso。结果证明它确实是必要的,原因超出了我的理解范围。


1
我已经添加了 androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1' 但错误仍然存在。 - HendraWD

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接