Java Hamcrest bug是否使用了or和null或不正确的用法?
我震惊地发现,有这样的事情:Java Hamcrest bug是否使用了or和null或不正确的用法?,java,junit,hamcrest,Java,Junit,Hamcrest,我震惊地发现,有这样的事情: 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
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-匹配如果任何匹配器匹配,短路(如Java | |)
比如:
assertThat(value, anyOf(equalTo(1), equalTo(2)));
有时间做一些调试 问题是
other()
生成一个CombinableMatcher
,它扩展了TypeSafeDiagnostingMatcher
。后者自动拒绝null
总之,Matcher
的类型参数实际上只是一个建议,而不是一个要求,所以这个超类是非常不安全的
编辑:
这里有一个bug报告()。我想它永远不会被修复