updated Unit test to add assertions examples
This commit is contained in:
parent
33474c6096
commit
587d69af32
|
@ -1,9 +1,11 @@
|
||||||
package com.baeldung.checkinterface;
|
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.assertFalse;
|
||||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
import java.util.HashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
|
@ -29,6 +31,7 @@ public class CheckInterfaceUnitTest {
|
||||||
ChildClass2 childClass2 = new ChildClass2();
|
ChildClass2 childClass2 = new ChildClass2();
|
||||||
List<Class<?>> interfaces = Arrays.asList(childClass2.getClass().getInterfaces());
|
List<Class<?>> interfaces = Arrays.asList(childClass2.getClass().getInterfaces());
|
||||||
|
|
||||||
|
assertEquals(1, interfaces.size());
|
||||||
assertTrue(interfaces.contains(ChildInterface2.class));
|
assertTrue(interfaces.contains(ChildInterface2.class));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -41,6 +44,15 @@ public class CheckInterfaceUnitTest {
|
||||||
assertFalse(interfaces.contains(MasterInterface.class));
|
assertFalse(interfaces.contains(MasterInterface.class));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenUsingReflectionGetInterfacesRecursively_thenParentInterfaceIsFound() {
|
||||||
|
|
||||||
|
Set<Class<?>> interfaces = getAllExtendedOrImplementedInterfacesRecursively(ChildClass2.class);
|
||||||
|
|
||||||
|
assertTrue(interfaces.contains(ChildInterface2.class));
|
||||||
|
assertTrue(interfaces.contains(MasterInterface.class));
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenUsingReflectionIsAssignableFrom_thenDirectlyImplementedInterfaceIsFound() {
|
public void whenUsingReflectionIsAssignableFrom_thenDirectlyImplementedInterfaceIsFound() {
|
||||||
|
|
||||||
|
@ -124,4 +136,19 @@ public class CheckInterfaceUnitTest {
|
||||||
|
|
||||||
assertTrue(interfaces.contains(MasterInterface.class));
|
assertTrue(interfaces.contains(MasterInterface.class));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static Set<Class<?>> getAllExtendedOrImplementedInterfacesRecursively(Class<?> clazz) {
|
||||||
|
|
||||||
|
Set<Class<?>> res = new HashSet<Class<?>>();
|
||||||
|
Class<?>[] interfaces = clazz.getInterfaces();
|
||||||
|
|
||||||
|
if (interfaces.length > 0) {
|
||||||
|
res.addAll(Arrays.asList(interfaces));
|
||||||
|
for (Class<?> interfaze : interfaces) {
|
||||||
|
res.addAll(getAllExtendedOrImplementedInterfacesRecursively(interfaze));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return res;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue