|
| 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