【问题标题】:Unresolved reference: Matchers未解决的参考:匹配器
【发布时间】:2018-10-06 08:22:27
【问题描述】:

当我导入 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 行会使项目编译并运行测试。

【问题讨论】:

    标签: android junit kotlin hamcrest


    【解决方案1】:

    就我而言,我在运行任务 :compileTestKotlin 时收到了编译器错误“未解析的引用:匹配器”。事实证明,我已将 hamcrest 依赖项声明为 testRuntimeOnly。将其更改为 testImplementation 解决了该问题。

    testImplementation("org.hamcrest:hamcrest:2.2")
    

    查看文档here 很有趣。

    【讨论】:

      【解决方案2】:

      我也有同样的问题。我发现我只是在 instrumented 测试中遇到了这个问题。非仪器测试能够导入并正常运行。

      解决这个问题的方法是将androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' 添加到我的依赖项中。

      我已经删除了这个认为没有必要的想法,因为我的测试都没有明确使用 Espresso。事实证明是,原因超出了我的理解。

      【讨论】:

      • 我有androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1',但错误仍然存​​在
      猜你喜欢
      • 1970-01-01
      • 2019-04-05
      • 1970-01-01
      • 2021-08-17
      • 2016-12-14
      • 2020-05-27
      • 2019-11-16
      • 2021-01-24
      • 2023-03-25
      相关资源
      最近更新 更多