diff --git a/core-java/src/test/java/com/baeldung/java/conversion/IterableStreamConversionTest.java b/core-java/src/test/java/com/baeldung/java/conversion/IterableStreamConversionTest.java index 1271519179..f52f73558f 100644 --- a/core-java/src/test/java/com/baeldung/java/conversion/IterableStreamConversionTest.java +++ b/core-java/src/test/java/com/baeldung/java/conversion/IterableStreamConversionTest.java @@ -1,48 +1,32 @@ package com.baeldung.java.conversion; -import java.util.Arrays; -import java.util.Iterator; -import java.util.List; -import java.util.stream.Collectors; -import java.util.stream.Stream; -import java.util.stream.StreamSupport; - import org.junit.Assert; import org.junit.Test; +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.StreamSupport; + import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.containsInAnyOrder; import static org.hamcrest.collection.IsIterableContainingInOrder.contains; public class IterableStreamConversionTest { @Test public void givenIterable_whenConvertedToStream_thenNotNull() { - String[] names = {"Testing", "Iterable", "conversion", "to", "Stream"}; - StreamIterable iterable = new StreamIterable<>(names); + Iterable iterable = Arrays.asList("Testing", "Iterable", "conversion", "to", "Stream"); + Assert.assertNotNull(StreamSupport.stream(iterable.spliterator(), false)); } @Test public void whenConvertedToList_thenCorrect() { - String[] names = {"Testing", "Iterable", "conversion", "to", "Stream"}; - StreamIterable iterable = new StreamIterable<>(names); - Stream convertedStream = StreamSupport.stream(iterable.spliterator(), false); - List collected = convertedStream.map(String::toUpperCase).collect(Collectors.toList()); - assertThat(collected, contains("TESTING", "ITERABLE", "CONVERSION", "TO", "STREAM")); + Iterable iterable = Arrays.asList("Testing", "Iterable", "conversion", "to", "Stream"); + + List result = StreamSupport.stream(iterable.spliterator(), false) + .map(String::toUpperCase).collect(Collectors.toList()); + + assertThat(result, contains("TESTING", "ITERABLE", "CONVERSION", "TO", "STREAM")); } } - -class StreamIterable implements Iterable { - private List list; - - StreamIterable(T[] array) { - this.list = Arrays.asList(array); - } - - @Override - public Iterator iterator() { - return list.iterator(); - } - -}