即使我知道 foo 不为空这一事实,该断言也能编译但失败:
import static org.hamcrest.Matchers.is; // see http://stackoverflow.com/a/27256498/2848676
import static org.hamcrest.Matchers.not;
import static org.hamcrest.MatcherAssert.assertThat;
Run Code Online (Sandbox Code Playgroud)
...
assertThat(foo, is(not(null)));
Run Code Online (Sandbox Code Playgroud)
Mic*_*sky 10
根据经验,我发现这可以代替:
assertThat(foo, is(not(nullValue())));
Run Code Online (Sandbox Code Playgroud)
epo*_*pox 10
你的断言不起作用,因为你not(Matcher<T> matcher)用null匹配器调用。请改用排序:
assertThat(foo, notNullValue());
Run Code Online (Sandbox Code Playgroud)
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.notNullValue;
...
assertThat(foo, notNullValue());
Run Code Online (Sandbox Code Playgroud)
致谢@eee
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.nullValue;
...
assertThat(foo, not( nullValue() ));
Run Code Online (Sandbox Code Playgroud)
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.not;
...
assertThat(foo, not( (Foo)null ));
Run Code Online (Sandbox Code Playgroud)
这里需要进行类型转换,以免not(T value)与not(Matcher<T> matcher). 参考:http://hamcrest.org/JavaHamcrest/javadoc/1.3/org/hamcrest/Matchers.html
| 归档时间: |
|
| 查看次数: |
8334 次 |
| 最近记录: |