diff --git a/core-java-modules/core-java-lang-5/src/test/java/com/baeldung/checkinterface/CheckInterfaceUnitTest.java b/core-java-modules/core-java-lang-5/src/test/java/com/baeldung/checkinterface/CheckInterfaceUnitTest.java index ab607322d1..dcbbb7eb05 100644 --- a/core-java-modules/core-java-lang-5/src/test/java/com/baeldung/checkinterface/CheckInterfaceUnitTest.java +++ b/core-java-modules/core-java-lang-5/src/test/java/com/baeldung/checkinterface/CheckInterfaceUnitTest.java @@ -1,9 +1,11 @@ package com.baeldung.checkinterface; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; import java.util.Arrays; +import java.util.HashSet; import java.util.List; import java.util.Set; @@ -29,6 +31,7 @@ public class CheckInterfaceUnitTest { ChildClass2 childClass2 = new ChildClass2(); List> interfaces = Arrays.asList(childClass2.getClass().getInterfaces()); + assertEquals(1, interfaces.size()); assertTrue(interfaces.contains(ChildInterface2.class)); } @@ -41,6 +44,15 @@ public class CheckInterfaceUnitTest { assertFalse(interfaces.contains(MasterInterface.class)); } + @Test + public void whenUsingReflectionGetInterfacesRecursively_thenParentInterfaceIsFound() { + + Set> interfaces = getAllExtendedOrImplementedInterfacesRecursively(ChildClass2.class); + + assertTrue(interfaces.contains(ChildInterface2.class)); + assertTrue(interfaces.contains(MasterInterface.class)); + } + @Test public void whenUsingReflectionIsAssignableFrom_thenDirectlyImplementedInterfaceIsFound() { @@ -124,4 +136,19 @@ public class CheckInterfaceUnitTest { assertTrue(interfaces.contains(MasterInterface.class)); } + + static Set> getAllExtendedOrImplementedInterfacesRecursively(Class clazz) { + + Set> res = new HashSet>(); + Class[] interfaces = clazz.getInterfaces(); + + if (interfaces.length > 0) { + res.addAll(Arrays.asList(interfaces)); + for (Class interfaze : interfaces) { + res.addAll(getAllExtendedOrImplementedInterfacesRecursively(interfaze)); + } + } + + return res; + } }