How to compare two Streams in Java 8

后端 未结 6 1832
眼角桃花
眼角桃花 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:32

    You can assert the stream's content without creating a Stream<> expected.

    AssertJ has fluent and readable solutions for this.

    import static org.assertj.core.api.Assertions.assertThat;
    import java.util.stream.Stream;
    import org.junit.jupiter.api.Test;
    
    class MyTests {
        @Test
        void test() {
            Stream actual = Stream.of(0, 8, 15); // your thingUnderTest
    
            assertThat(actual).containsExactly(0, 8, 15);
        }
    }
    

提交回复
热议问题