unruly / java-8-matchers

Hamcrest Matchers for Java 8 features
MIT License
23 stars 5 forks source link

contains(..) is not null-safe #3

Closed runeflobakk closed 8 years ago

runeflobakk commented 8 years ago

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.

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) with java.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!