使用or-or和null或不正确使用的Hamcrest错误?

bil*_*.cn 7 java junit hamcrest

当遇到以下问题时,我感到震惊:

assertThat(null, either(is(nullValue())).or(notNullValue()));
Run Code Online (Sandbox Code Playgroud)

失败:

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)
Run Code Online (Sandbox Code Playgroud)

我不认为这种用法非常不寻常(我实际上是在尝试断言null或空映射)而且我找不到Hamcrest源代码的任何问题......

the*_*war 5

使用 anyOf

来自Hamcrest教程

anyOf - 匹配任何匹配器匹配,短路(如Java ||)

就像是:

assertThat(value, anyOf(equalTo(1), equalTo(2)));
Run Code Online (Sandbox Code Playgroud)