Hamcrest 错误与非此即彼和 null 或不正确的用法?
Hamcrest bug with either-or and null or incorrect usage?
当出现以下情况时,我感到很震惊:
assertThat(null, either(is(nullValue())).or(notNullValue()));
失败:
java.lang.AssertionError:
Expected: (is null or not null)
but: was null
at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:20)
at org.junit.Assert.assertThat(Assert.java:956)
at org.junit.Assert.assertThat(Assert.java:923)
at Demo.testName(Demo.java:12)
我不认为这种用法很不寻常(我实际上是在尝试断言 null 或空映射)并且我找不到 Hamcrest 源代码的任何错误...
使用anyOf
anyOf - matches if any matchers match, short circuits (like Java ||)
类似于:
assertThat(value, anyOf(equalTo(1), equalTo(2)));
有时间做一些调试。
问题是 either()
生成一个扩展 TypeSafeDiagnosingMatcher
的 CombinableMatcher
。后者自动拒绝 null.
恕我直言,Matcher
的类型参数实际上只是一个建议而不是要求,所以这个超级 class 真的很不安全...
编辑:
这是一份错误报告 (https://github.com/hamcrest/JavaHamcrest/issues/49)。我想它永远不会得到修复...
当出现以下情况时,我感到很震惊:
assertThat(null, either(is(nullValue())).or(notNullValue()));
失败:
java.lang.AssertionError:
Expected: (is null or not null)
but: was null
at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:20)
at org.junit.Assert.assertThat(Assert.java:956)
at org.junit.Assert.assertThat(Assert.java:923)
at Demo.testName(Demo.java:12)
我不认为这种用法很不寻常(我实际上是在尝试断言 null 或空映射)并且我找不到 Hamcrest 源代码的任何错误...
使用anyOf
anyOf - matches if any matchers match, short circuits (like Java ||)
类似于:
assertThat(value, anyOf(equalTo(1), equalTo(2)));
有时间做一些调试。
问题是 either()
生成一个扩展 TypeSafeDiagnosingMatcher
的 CombinableMatcher
。后者自动拒绝 null.
恕我直言,Matcher
的类型参数实际上只是一个建议而不是要求,所以这个超级 class 真的很不安全...
编辑:
这是一份错误报告 (https://github.com/hamcrest/JavaHamcrest/issues/49)。我想它永远不会得到修复...