tl;dr
your assert doesn't work, because you call not(Matcher<T> matcher)
with null
matcher. Use a shortcut, instead:
assertThat(foo, notNullValue());
the shortcut:
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.notNullValue;
...
assertThat(foo, notNullValue());
credits to @eee
the canonical form:
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.nullValue;
...
assertThat(foo, not( nullValue() ));
your (OP) approach:
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.not;
...
assertThat(foo, not( (Foo)null ));
The type casting is required here, in order not to confuse not(T value)
with not(Matcher<T> matcher)
.
REF: http://hamcrest.org/JavaHamcrest/javadoc/1.3/org/hamcrest/Matchers.html
Matchers.notNullValue()
. – Prefatory