SystemExceptionSelector { public SystemException getSystemException(final Exception e) { SystemException result = null; if (e instanceof BeanCreationException) { if (((NestedRuntimeException) e).getMostSpecificCause() != null) { Throwable ex = e; boolean found = false; while (ex.getCause() != null && !found) { if (ex.getCause() instanceof SystemException) { found = true; result = (SystemException) ex.getCause(); } ex = ex.getCause(); } } } return result; }  SystemException getSystemException(final Exception e);  }
@Test public void testGetSystemExceptionShouldReturnWithNullWhenExceptionMostSpecificCauseIsNull() { given(beanCreationException.getMostSpecificCause()).willReturn(null); SystemException actual = underTest.getSystemException(beanCreationException); assertNull(actual); }
@Test public void testGetSystemExceptionShouldReturnWithNullWhenExceptionCauseIsNull() { given(beanCreationException.getMostSpecificCause()).willReturn(invalidPropertyException); given(beanCreationException.getCause()).willReturn(null); SystemException actual = underTest.getSystemException(beanCreationException); assertNull(actual); }
@Test public void testGetSystemExceptionShouldReturnWithNullWhenExceptionCauseNotContainSystemException() { given(beanCreationException.getMostSpecificCause()).willReturn(applicationException); given(beanCreationException.getCause()).willReturn(applicationException, applicationException); SystemException actual = underTest.getSystemException(beanCreationException); assertNull(actual); }
@Test public void testGetSystemExceptionShouldReturnWithSystemExceptionWhenExceptionCauseContainsSystemException() { given(beanCreationException.getMostSpecificCause()).willReturn(invalidPropertyException); given(beanCreationException.getCause()).willReturn(invalidPropertyException, invalidPropertyException); SystemException actual = underTest.getSystemException(beanCreationException); assertNotNull(actual); }