Skip to content

Commit 8f938c5

Browse files
authored
Merge pull request eugenp#6093 from urvyagrawal/BAEL-2543
Adding files for BAEL-2543
2 parents b395dc1 + 7f501f1 commit 8f938c5

File tree

6 files changed

+76
-0
lines changed

6 files changed

+76
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.baeldung.keyword;
2+
3+
public class Circle extends Round implements Shape {
4+
5+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package com.baeldung.keyword;
2+
3+
public class Ring extends Round {
4+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package com.baeldung.keyword;
2+
3+
public class Round {
4+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package com.baeldung.keyword;
2+
3+
public interface Shape {
4+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package com.baeldung.keyword;
2+
3+
public class Triangle implements Shape {
4+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.baeldung.keyword.test;
2+
3+
import org.junit.Assert;
4+
import org.junit.jupiter.api.Test;
5+
6+
import com.baeldung.keyword.Circle;
7+
import com.baeldung.keyword.Ring;
8+
import com.baeldung.keyword.Round;
9+
import com.baeldung.keyword.Shape;
10+
import com.baeldung.keyword.Triangle;
11+
12+
public class InstanceOfUnitTest {
13+
14+
@Test
15+
public void giveWhenInstanceIsCorrect_thenReturnTrue() {
16+
Ring ring = new Ring();
17+
Assert.assertTrue("ring is instance of Round ", ring instanceof Round);
18+
}
19+
20+
@Test
21+
public void giveWhenObjectIsInstanceOfType_thenReturnTrue() {
22+
Circle circle = new Circle();
23+
Assert.assertTrue("circle is instance of Circle ", circle instanceof Circle);
24+
}
25+
26+
27+
@Test
28+
public void giveWhenInstanceIsOfSubtype_thenReturnTrue() {
29+
Circle circle = new Circle();
30+
Assert.assertTrue("circle is instance of Round", circle instanceof Round);
31+
}
32+
33+
@Test
34+
public void giveWhenTypeIsInterface_thenReturnTrue() {
35+
Circle circle = new Circle();
36+
Assert.assertTrue("circle is instance of Shape", circle instanceof Shape);
37+
}
38+
39+
@Test
40+
public void giveWhenTypeIsOfObjectType_thenReturnTrue() {
41+
Thread thread = new Thread();
42+
Assert.assertTrue("thread is instance of Object", thread instanceof Object);
43+
}
44+
45+
@Test
46+
public void giveWhenInstanceValueIsNull_thenReturnFalse() {
47+
Circle circle = null;
48+
Assert.assertFalse("circle is instance of Round", circle instanceof Round);
49+
}
50+
51+
@Test
52+
public void giveWhenComparingClassInDiffHierarchy_thenCompilationError() {
53+
// Assert.assertFalse("circle is instance of Triangle", circle instanceof Triangle);
54+
}
55+
}

0 commit comments

Comments
 (0)