changed example for finder of root cause exception

This commit is contained in:
Marcos Lopez Gonzalez
2019-04-29 19:58:51 +02:00
committed by mikr
parent 0d9608507b
commit 89dddaebe8
2 changed files with 94 additions and 31 deletions

View File

@@ -4,6 +4,8 @@ import com.google.common.base.Throwables;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.junit.jupiter.api.Test;
import java.time.format.DateTimeParseException;
import static com.baeldung.exceptions.RootCauseFinder.*;
import static org.junit.jupiter.api.Assertions.assertTrue;
@@ -13,38 +15,65 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
public class RootCauseFinderTest {
@Test
public void givenNestedException_whenFindingRootCauseUsingJava_thenRootCauseFound() {
public void givenWrongFormatDate_whenFindingRootCauseUsingJava_thenRootCauseFound() {
try {
IntParser.parse("text");
} catch (InvalidNumber ex) {
assertTrue(findCauseUsingPlainJava(ex) instanceof NumberFormatException);
AgeCalculator.calculateAge("010102");
} catch (CalculationException ex) {
assertTrue(findCauseUsingPlainJava(ex) instanceof DateTimeParseException);
}
}
@Test
public void givenNonNestedException_whenFindingRootCauseUsingJava_thenRootCauseFound() {
public void givenOutOfRangeDate_whenFindingRootCauseUsingJava_thenRootCauseFound() {
try {
IntParser.parse(null);
AgeCalculator.calculateAge("2020-04-04");
} catch (CalculationException ex) {
assertTrue(findCauseUsingPlainJava(ex) instanceof DateOutOfRangeException);
}
}
@Test
public void givenNullDate_whenFindingRootCauseUsingJava_thenRootCauseFound() {
try {
AgeCalculator.calculateAge(null);
} catch (Exception ex) {
assertTrue(findCauseUsingPlainJava(ex) instanceof IllegalArgumentException);
}
}
@Test
public void givenNestedException_whenFindingRootCauseUsingApacheCommons_thenRootCauseFound() {
public void givenWrongFormatDate_whenFindingRootCauseUsingApacheCommons_thenRootCauseFound() {
try {
IntParser.parse("text");
} catch (InvalidNumber ex) {
assertTrue(ExceptionUtils.getRootCause(ex) instanceof NumberFormatException);
AgeCalculator.calculateAge("010102");
} catch (CalculationException ex) {
assertTrue(ExceptionUtils.getRootCause(ex) instanceof DateTimeParseException);
}
}
@Test
public void givenNestedException_whenFindingRootCauseUsingGuava_thenRootCauseFound() {
public void givenOutOfRangeDate_whenFindingRootCauseUsingApacheCommons_thenRootCauseFound() {
try {
IntParser.parse("text");
} catch (InvalidNumber ex) {
assertTrue(Throwables.getRootCause(ex) instanceof NumberFormatException);
AgeCalculator.calculateAge("2020-04-04");
} catch (CalculationException ex) {
assertTrue(ExceptionUtils.getRootCause(ex) instanceof DateOutOfRangeException);
}
}
@Test
public void givenWrongFormatDate_whenFindingRootCauseUsingGuava_thenRootCauseFound() {
try {
AgeCalculator.calculateAge("010102");
} catch (CalculationException ex) {
assertTrue(Throwables.getRootCause(ex) instanceof DateTimeParseException);
}
}
@Test
public void givenOutOfRangeDate_whenFindingRootCauseUsingGuava_thenRootCauseFound() {
try {
AgeCalculator.calculateAge("2020-04-04");
} catch (CalculationException ex) {
assertTrue(Throwables.getRootCause(ex) instanceof DateOutOfRangeException);
}
}