What would be a good way to compare two Stream
instances in Java 8 and find out whether they have the same elements, specifically for purposes of unit testing?<
Collecting the stream under test (as you show) is a straightforward and effective way of performing the test. You may create the list of expected results in the easiest way available, which might not be collecting a stream.
Alternatively, with most libraries for creating mock collaborators, one could mock a Consumer
that "expects" a series of accept()
calls with particular elements. Consume the Stream
with it, and then "verify" that its configured expectations were met.