【问题标题】:Assertion, is null or is an instance of specific Class断言,为空或者是特定类的实例
【发布时间】:2012-11-30 11:31:23
【问题描述】:

我觉得很简单

 Assert.assertThat(
                iterator.next(),
                Matchers.either(Matchers.nullValue()).or(
                        Matchers.instanceOf(Double.class)));

所以我想 ensre next() 返回两个之一,NULL 或 Double 的实例。 某样东西

Assert.assertTrue(next == null || next instanceof Double);

测试运行 (assertThat) 结果...

 java.lang.NoSuchMethodError: org.hamcrest.Matcher.describeMismatch(Ljava/lang/Object;Lorg/hamcrest/Description;)V
at org.hamcrest.core.CombinableMatcher.matchesSafely(CombinableMatcher.java:17)
at org.hamcrest.TypeSafeDiagnosingMatcher.matches(TypeSafeDiagnosingMatcher.java:55)
at org.junit.Assert.assertThat(Assert.java:772)
at org.junit.Assert.assertThat(Assert.java:738

为什么?以及如何解决?

编辑

你是对的,与来自 Mockito 的 hamcrest lib 发生了冲突。我已经修好了。 没有错误,但是断言异常失败,看看:

 java.lang.AssertionError: 
 Expected: (null or an instance of java.lang.Double)
 got: null
at org.junit.Assert.assertThat(Assert.java:780)
at org.junit.Assert.assertThat(Assert.java:738)

预期为 Null,结果为 null,测试失败。

【问题讨论】:

标签: java unit-testing junit assertions hamcrest


【解决方案1】:

NoSuchMethodError 表示有关编译时和运行时环境之间差异的问题。一个典型的原因是您在运行时有不同版本的 JAR。也许你有两个 Hamcrest JAR,但一个优先。仔细检查编译时和运行时类路径。

【讨论】:

    【解决方案2】:

    我建议您有多个冲突的 Hamcrest 依赖项。检查您的构建路径/maven 依赖项。

    如果您在 java.lang.NoSuchMethodError: org.hamcrest.Matcher.describeMismatch 上执行搜索,您将获得以下完全相同问题的条目列表:

    Did you search before posting this question?

    第一个结果在我看来是正确的:

    First search result

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-10-01
      • 2012-09-06
      • 1970-01-01
      • 2018-01-18
      • 2015-01-19
      • 1970-01-01
      • 2017-06-18
      • 1970-01-01
      相关资源
      最近更新 更多