[instanceOf-stream] Checking instanceof in Java Stream (#13419)
This commit is contained in:
parent
a70afe6a9f
commit
c0be80a64a
@ -1,55 +1,67 @@
|
|||||||
package com.baeldung.keyword;
|
package com.baeldung.keyword;
|
||||||
|
|
||||||
import org.junit.Assert;
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
import com.baeldung.keyword.Circle;
|
import java.util.List;
|
||||||
import com.baeldung.keyword.Ring;
|
import java.util.stream.Collectors;
|
||||||
import com.baeldung.keyword.Round;
|
import java.util.stream.Stream;
|
||||||
import com.baeldung.keyword.Shape;
|
|
||||||
import com.baeldung.keyword.Triangle;
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
public class InstanceOfUnitTest {
|
public class InstanceOfUnitTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void giveWhenInstanceIsCorrect_thenReturnTrue() {
|
void giveWhenInstanceIsCorrect_thenReturnTrue() {
|
||||||
Ring ring = new Ring();
|
Ring ring = new Ring();
|
||||||
Assert.assertTrue("ring is instance of Round ", ring instanceof Round);
|
assertTrue(ring instanceof Round);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void giveWhenObjectIsInstanceOfType_thenReturnTrue() {
|
void giveWhenObjectIsInstanceOfType_thenReturnTrue() {
|
||||||
Circle circle = new Circle();
|
Circle circle = new Circle();
|
||||||
Assert.assertTrue("circle is instance of Circle ", circle instanceof Circle);
|
assertTrue(circle instanceof Circle);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void giveWhenInstanceIsOfSubtype_thenReturnTrue() {
|
void giveWhenInstanceIsOfSubtype_thenReturnTrue() {
|
||||||
Circle circle = new Circle();
|
Circle circle = new Circle();
|
||||||
Assert.assertTrue("circle is instance of Round", circle instanceof Round);
|
assertTrue(circle instanceof Round);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void giveWhenTypeIsInterface_thenReturnTrue() {
|
void giveWhenTypeIsInterface_thenReturnTrue() {
|
||||||
Circle circle = new Circle();
|
Circle circle = new Circle();
|
||||||
Assert.assertTrue("circle is instance of Shape", circle instanceof Shape);
|
assertTrue(circle instanceof Shape);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void giveWhenTypeIsOfObjectType_thenReturnTrue() {
|
void giveWhenTypeIsOfObjectType_thenReturnTrue() {
|
||||||
Thread thread = new Thread();
|
Thread thread = new Thread();
|
||||||
Assert.assertTrue("thread is instance of Object", thread instanceof Object);
|
assertTrue(thread instanceof Object);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void giveWhenInstanceValueIsNull_thenReturnFalse() {
|
void giveWhenInstanceValueIsNull_thenReturnFalse() {
|
||||||
Circle circle = null;
|
Circle circle = null;
|
||||||
Assert.assertFalse("circle is instance of Round", circle instanceof Round);
|
assertFalse(circle instanceof Round);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void giveWhenComparingClassInDiffHierarchy_thenCompilationError() {
|
void giveWhenComparingClassInDiffHierarchy_thenCompilationError() {
|
||||||
// Assert.assertFalse("circle is instance of Triangle", circle instanceof Triangle);
|
//assertFalse( circle instanceof Triangle);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
@Test
|
||||||
|
void giveWhenStream_whenCastWithoutInstanceOfChk_thenGetException() {
|
||||||
|
Stream<Round> 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<Round> roundStream = Stream.of(new Ring(), new Ring(), new Circle());
|
||||||
|
List<Ring> ringList = roundStream.filter(it -> it instanceof Ring).map(it -> (Ring) it).collect(Collectors.toList());
|
||||||
|
assertEquals(2, ringList.size());
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user