A Stream may contain nulls (though whether that's a good idea is up for debate ;) ), but StreamMatchers.contains(..) is not null safe. A workaround is to collect the steam to e.g. a List and use Hamcrest's Matchers.contains(..), which handles expected null-elements. The workaround also demonstrates that StreamMatchers.contains(..) should indeed handle null-elements.
A Stream may contain
nulls
(though whether that's a good idea is up for debate ;) ), butStreamMatchers.contains(..)
is not null safe. A workaround is to collect the steam to e.g. a List and use Hamcrest'sMatchers.contains(..)
, which handles expected null-elements. The workaround also demonstrates thatStreamMatchers.contains(..)
should indeed handle null-elements.The sollution is simple. Just replace the
nextExpected.equals(nextActual)
(https://github.com/unruly/java-8-matchers/blob/master/src/main/java/co/unruly/matchers/StreamMatchers.java#L650) withjava.util.Objects.equals(nextExpected, nextActual)
, and it handles expected null-elements.I have implemented the fix with testcase that I will issue a pull request for.
Thanks for making the library!