How to compare two Streams in Java 8

后端 未结 6 1834
眼角桃花
眼角桃花 2020-12-03 06:38

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?<

6条回答
  •  执笔经年
    2020-12-03 07:26

    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.

提交回复
热议问题