Skip to content

Commit 9081c08

Browse files
nabylapivovarit
authored andcommitted
Add stream supplier test (eugenp#2631)
1 parent f384e46 commit 9081c08

1 file changed

Lines changed: 35 additions & 0 deletions

File tree

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.baeldung.stream;
2+
3+
import static org.junit.Assert.fail;
4+
5+
import java.util.Optional;
6+
import java.util.function.Supplier;
7+
import java.util.stream.Stream;
8+
9+
import org.junit.Test;
10+
11+
public class SupplierStreamTest {
12+
13+
@Test(expected = IllegalStateException.class)
14+
public void givenStream_whenStreamUsedTwice_thenThrowException() {
15+
Stream<String> stringStream = Stream.of("A", "B", "C", "D");
16+
Optional<String> result1 = stringStream.findAny();
17+
System.out.println(result1.get());
18+
Optional<String> result2 = stringStream.findFirst();
19+
System.out.println(result2.get());
20+
}
21+
22+
@Test
23+
public void givenStream_whenUsingSupplier_thenNoExceptionIsThrown() {
24+
try {
25+
Supplier<Stream<String>> streamSupplier = () -> Stream.of("A", "B", "C", "D");
26+
Optional<String> result1 = streamSupplier.get().findAny();
27+
System.out.println(result1.get());
28+
Optional<String> result2 = streamSupplier.get().findFirst();
29+
System.out.println(result2.get());
30+
} catch (IllegalStateException e) {
31+
fail();
32+
}
33+
}
34+
35+
}

0 commit comments

Comments
 (0)