BindingValidator { public <T> void validate(T configurationBean, Class<T> type) { LOG.debug("Validating configuration bean of type " + type); for (Method declaredMethod : type.getDeclaredMethods()) { try { LOG.debug("Validating method: " + declaredMethod.getName()); declaredMethod.invoke(configurationBean); } catch (InvocationTargetException e) { if (e.getCause() != null) { if (e.getCause() instanceof NoSuchElementException) { throw (NoSuchElementException) e.getCause(); } if (e.getCause() instanceof IllegalArgumentException) { throw (IllegalArgumentException) e.getCause(); } } throw new IllegalStateException("Can't bind method " + declaredMethod.getName(), e); } catch (IllegalAccessException e) { throw new IllegalStateException("Can't bind method " + declaredMethod.getName(), e); } } }  void validate(T configurationBean, Class<T> type);  }
@Test void invokesAllMethods() { when(configPojo.otherSetting()).thenReturn(Collections.<Boolean>emptyList()); bindingValidator.validate(configPojo, ConfigPojo.class); verify(configPojo, times(1)).someSetting(); verify(configPojo, times(1)).otherSetting(); }
@Test void propagatesNoSuchElementExceptionsFromInvocation() { when(configPojo.someSetting()).thenThrow(NoSuchElementException.class); assertThatThrownBy(() -> bindingValidator.validate(configPojo, ConfigPojo.class)).isExactlyInstanceOf(NoSuchElementException.class); }
@Test void propagatesIllegalArgumentExceptionsFromInvocation() { when(configPojo.someSetting()).thenThrow(IllegalArgumentException.class); assertThatThrownBy(() -> bindingValidator.validate(configPojo, ConfigPojo.class)).isExactlyInstanceOf(IllegalArgumentException.class); }
@Test void throwsOnOtherExceptions() { when(configPojo.someSetting()).thenThrow(IllegalStateException.class); assertThatThrownBy(() -> bindingValidator.validate(configPojo, ConfigPojo.class)).isExactlyInstanceOf(IllegalStateException.class); }