### Question:
RelationResolver { static Class<?> findMostSpecificAncestor(Set<Class<?>> parents, Class<?> child) { Class<?> nearest = null; int minDistance = Integer.MAX_VALUE; for (Class<?> parent : parents) { int distance = calculateInheritanceDistance(parent, child); if (distance >= 0 && distance < minDistance) { nearest = parent; minDistance = distance; } } return nearest; } private  RelationResolver(); static int calculateInheritanceDistance(Class<?> parent, Class<?> child);  }### Answer:
@Test public void findMostSpecificAncestorEmpty() { Set<Class<?>> parents = Collections.emptySet(); Class<?> child = Object.class; Class<?> ancestor = RelationResolver.findMostSpecificAncestor(parents, child); assertThat(ancestor, nullValue()); }
@Test public void findMostSpecificAncestorNotFound() { Set<Class<?>> parents = new HashSet<>(asList(String.class, Integer.class, Void.class)); Class<?> child = Double.class; Class<?> ancestor = RelationResolver.findMostSpecificAncestor(parents, child); assertThat(ancestor, nullValue()); }
@Test(expected = IllegalArgumentException.class) public void findMostSpecificAncestorAssignableInterfaces() { Set<Class<?>> parents = Collections.singleton(Serializable.class); Class<?> child = Double.class; RelationResolver.findMostSpecificAncestor(parents, child); }
@Test public void findMostSpecificAncestor() { Set<Class<?>> parents = new HashSet<>(asList(String.class, BigDecimal.class, Object.class, Number.class)); Class<?> child = Number.class; Class<?> ancestor = RelationResolver.findMostSpecificAncestor(parents, child); assertEquals(Number.class, ancestor); }