From c0be80a64afc8138d9df28d376b52cbf481cece5 Mon Sep 17 00:00:00 2001 From: Kai Yuan Date: Sat, 4 Feb 2023 17:12:16 +0100 Subject: [PATCH] [instanceOf-stream] Checking instanceof in Java Stream (#13419) --- .../baeldung/keyword/InstanceOfUnitTest.java | 58 +++++++++++-------- 1 file changed, 35 insertions(+), 23 deletions(-) diff --git a/core-java-modules/core-java-lang-operators/src/test/java/com/baeldung/keyword/InstanceOfUnitTest.java b/core-java-modules/core-java-lang-operators/src/test/java/com/baeldung/keyword/InstanceOfUnitTest.java index 0e082b69d3..6ba8ed024c 100644 --- a/core-java-modules/core-java-lang-operators/src/test/java/com/baeldung/keyword/InstanceOfUnitTest.java +++ b/core-java-modules/core-java-lang-operators/src/test/java/com/baeldung/keyword/InstanceOfUnitTest.java @@ -1,55 +1,67 @@ package com.baeldung.keyword; -import org.junit.Assert; import org.junit.jupiter.api.Test; -import com.baeldung.keyword.Circle; -import com.baeldung.keyword.Ring; -import com.baeldung.keyword.Round; -import com.baeldung.keyword.Shape; -import com.baeldung.keyword.Triangle; +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import static org.junit.jupiter.api.Assertions.*; public class InstanceOfUnitTest { @Test - public void giveWhenInstanceIsCorrect_thenReturnTrue() { + void giveWhenInstanceIsCorrect_thenReturnTrue() { Ring ring = new Ring(); - Assert.assertTrue("ring is instance of Round ", ring instanceof Round); + assertTrue(ring instanceof Round); } @Test - public void giveWhenObjectIsInstanceOfType_thenReturnTrue() { + void giveWhenObjectIsInstanceOfType_thenReturnTrue() { Circle circle = new Circle(); - Assert.assertTrue("circle is instance of Circle ", circle instanceof Circle); + assertTrue(circle instanceof Circle); } - + @Test - public void giveWhenInstanceIsOfSubtype_thenReturnTrue() { + void giveWhenInstanceIsOfSubtype_thenReturnTrue() { Circle circle = new Circle(); - Assert.assertTrue("circle is instance of Round", circle instanceof Round); + assertTrue(circle instanceof Round); } @Test - public void giveWhenTypeIsInterface_thenReturnTrue() { + void giveWhenTypeIsInterface_thenReturnTrue() { Circle circle = new Circle(); - Assert.assertTrue("circle is instance of Shape", circle instanceof Shape); + assertTrue(circle instanceof Shape); } - + @Test - public void giveWhenTypeIsOfObjectType_thenReturnTrue() { + void giveWhenTypeIsOfObjectType_thenReturnTrue() { Thread thread = new Thread(); - Assert.assertTrue("thread is instance of Object", thread instanceof Object); + assertTrue(thread instanceof Object); } @Test - public void giveWhenInstanceValueIsNull_thenReturnFalse() { + void giveWhenInstanceValueIsNull_thenReturnFalse() { Circle circle = null; - Assert.assertFalse("circle is instance of Round", circle instanceof Round); + assertFalse(circle instanceof Round); } @Test - public void giveWhenComparingClassInDiffHierarchy_thenCompilationError() { - // Assert.assertFalse("circle is instance of Triangle", circle instanceof Triangle); + void giveWhenComparingClassInDiffHierarchy_thenCompilationError() { + //assertFalse( circle instanceof Triangle); } -} + + @Test + void giveWhenStream_whenCastWithoutInstanceOfChk_thenGetException() { + Stream roundStream = Stream.of(new Ring(), new Ring(), new Circle()); + assertThrows(ClassCastException.class, () -> roundStream.map(it -> (Ring) it).collect(Collectors.toList())); + } + + @Test + void giveWhenStream_whenCastAfterInstanceOfChk_thenGetExpectedResult() { + Stream roundStream = Stream.of(new Ring(), new Ring(), new Circle()); + List ringList = roundStream.filter(it -> it instanceof Ring).map(it -> (Ring) it).collect(Collectors.toList()); + assertEquals(2, ringList.size()); + } +} \ No newline at end of file