Skip to content

Commit

Permalink
[tests] assertException takes care of the root cause.
Browse files Browse the repository at this point in the history
Signed-off-by: Stéphane Galland <galland@arakhne.org>
  • Loading branch information
gallandarakhneorg committed Mar 8, 2020
1 parent 9ca46cf commit 07d7535
Showing 1 changed file with 9 additions and 6 deletions.
Expand Up @@ -39,6 +39,7 @@
import com.google.common.base.Objects;
import com.google.common.base.Predicate;
import com.google.common.base.Strings;
import com.google.common.base.Throwables;
import com.google.common.collect.Iterables;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EObject;
Expand Down Expand Up @@ -991,8 +992,9 @@ public static void assertException(Class<? extends Throwable> expected, Code cod
code.run();
fail("Expecting exception of type " + expected.getName());
} catch (Throwable ex) {
if (!expected.isAssignableFrom(ex.getClass())) {
fail("Expecting exception of type " + expected.getName() + ", but got " + ex.getClass().getName(), ex);
final Throwable cause = Throwables.getRootCause(ex);
if (!expected.isAssignableFrom(cause.getClass())) {
fail("Expecting exception of type " + expected.getName() + ", but got " + cause.getClass().getName(), cause);
}
}
}
Expand All @@ -1009,12 +1011,13 @@ public static void assertException(Class<? extends Throwable> expected, Code cod
public static <T extends Throwable> ExceptionChecker<T> whenException(Class<T> expected, Code code) throws Exception {
try {
code.run();
fail("Expecting exception of type " + expected.getName() + ", but not exception is known");
fail("Expecting exception of type " + expected.getName() + ", but no exception is known");
} catch (Throwable ex) {
if (!expected.isAssignableFrom(ex.getClass())) {
fail("Expecting exception of type " + expected.getName() + ", but get " + ex.getClass().getName(), ex);
final Throwable cause = Throwables.getRootCause(ex);
if (!expected.isAssignableFrom(cause.getClass())) {
fail("Expecting exception of type " + expected.getName() + ", but get " + cause.getClass().getName(), cause);
} else {
return new ExceptionChecker<>((T) ex);
return new ExceptionChecker<>((T) cause);
}
}
return null;
Expand Down

0 comments on commit 07d7535

Please sign in to comment.