RootCauseFinder improvements

This commit is contained in:
Marcos Lopez Gonzalez
2019-05-03 20:13:38 +02:00
parent 6da90b24b0
commit c2d94513d8
3 changed files with 23 additions and 2 deletions
@@ -10,10 +10,13 @@ import java.util.Objects;
*/
public class RootCauseFinder {
private RootCauseFinder() {
}
public static Throwable findCauseUsingPlainJava(Throwable throwable) {
Objects.requireNonNull(throwable);
Throwable rootCause = throwable;
while (rootCause.getCause() != null) {
while (rootCause.getCause() != null && rootCause.getCause() != rootCause) {
rootCause = rootCause.getCause();
}
return rootCause;
@@ -75,6 +75,15 @@ public class RootCauseFinderTest {
}
}
@Test
public void givenNullDate_whenFindingRootCauseUsingApacheCommons_thenRootCauseNotFound() {
try {
AgeCalculator.calculateAge(null);
} catch (Exception ex) {
assertTrue(ExceptionUtils.getRootCause(ex) instanceof IllegalArgumentException);
}
}
@Test
public void givenWrongFormatDate_whenFindingRootCauseUsingGuava_thenRootCauseFound() {
try {
@@ -93,4 +102,13 @@ public class RootCauseFinderTest {
}
}
@Test
public void givenNullDate_whenFindingRootCauseUsingGuava_thenRootCauseFound() {
try {
AgeCalculator.calculateAge(null);
} catch (Exception ex) {
assertTrue(Throwables.getRootCause(ex) instanceof IllegalArgumentException);
}
}
}