From 9081c089f69977dc53435c37ddca98bf0bb0baa8 Mon Sep 17 00:00:00 2001 From: nabyla <31042612+nabyla@users.noreply.github.com> Date: Sun, 17 Sep 2017 08:20:58 +0100 Subject: [PATCH] Add stream supplier test (#2631) --- .../baeldung/stream/SupplierStreamTest.java | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 core-java-8/src/test/java/com/baeldung/stream/SupplierStreamTest.java diff --git a/core-java-8/src/test/java/com/baeldung/stream/SupplierStreamTest.java b/core-java-8/src/test/java/com/baeldung/stream/SupplierStreamTest.java new file mode 100644 index 0000000000..d78c9fca35 --- /dev/null +++ b/core-java-8/src/test/java/com/baeldung/stream/SupplierStreamTest.java @@ -0,0 +1,35 @@ +package com.baeldung.stream; + +import static org.junit.Assert.fail; + +import java.util.Optional; +import java.util.function.Supplier; +import java.util.stream.Stream; + +import org.junit.Test; + +public class SupplierStreamTest { + + @Test(expected = IllegalStateException.class) + public void givenStream_whenStreamUsedTwice_thenThrowException() { + Stream stringStream = Stream.of("A", "B", "C", "D"); + Optional result1 = stringStream.findAny(); + System.out.println(result1.get()); + Optional result2 = stringStream.findFirst(); + System.out.println(result2.get()); + } + + @Test + public void givenStream_whenUsingSupplier_thenNoExceptionIsThrown() { + try { + Supplier> streamSupplier = () -> Stream.of("A", "B", "C", "D"); + Optional result1 = streamSupplier.get().findAny(); + System.out.println(result1.get()); + Optional result2 = streamSupplier.get().findFirst(); + System.out.println(result2.get()); + } catch (IllegalStateException e) { + fail(); + } + } + +} \ No newline at end of file