【问题标题】:Custom hamcrest matcher in unit tests单元测试中的自定义 hamcrest 匹配器
【发布时间】:2013-08-11 17:07:19
【问题描述】:

在实现自定义 hamcrest 匹配器以在 Grails 中使用时遇到问题。使用我的匹配器运行测试失败:

java.lang.NoSuchMethodError: org.hamcrest.Matcher.describeMismatch(Ljava/lang/Object;Lorg/hamcrest/Description;)V

经过一番谷歌搜索,这似乎是由加载库的顺序引起的:JUnit 和 Hamcrest。我在 BuildConfig.groovy 中添加了以下依赖项:

dependencies {
  test 'org.hamcrest:hamcrest-all:1.3'
}

根据我能够找到的说明,这可以通过确保在 JUnit 类之前加载 hamcrest 类来解决。我不知道如何在 Grails 中实现这一点,也不知道如何以任何其他方式解决这个问题。

使用 Grails 2.2.1

【问题讨论】:

    标签: grails junit classloader hamcrest matcher


    【解决方案1】:

    这取决于您使用的 JUnit 版本。您可以通过查看lib/junit/junit/jars 来检查这一点。我认为默认情况下这是 4.10。如果是这样,请升级到 4.11。您可以通过在 BuildConfig.groovy 中明确指定它来做到这一点,我相信:

    dependencies {
      test 'junit:junit:4.11'
      test 'org.hamcrest:hamcrest-all:1.3'
    }
    

    作为解释,在 4.11 之前,一个版本的 hamcrest 库包含在 JUnit 发行版中。 4.11 不再是这种情况,您有时会遇到类版本之间的不匹配。

    【讨论】:

    • 做到了!只需要记住在更改后刷新依赖项即可。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-05
    • 1970-01-01
    相关资源
    最近更新 更多