Issue | Fix Version | Assignee | Priority | Status | Resolution | Resolved In Build |
---|---|---|---|---|---|---|
JDK-8142625 | emb-9 | Kirill Zhaldybin | P3 | Resolved | Fixed | team |
in case expectedException isn't null, but runnable::run hasn't thrown any exception, runAndCheckException will throw AssertionError which will be caught in catch block and exception type will be checked w/ expectedException.
that causes an extra (incorrect) exception wrapping, and also can cause false negative result (if expectedException == AssertionError.class)
public static void runAndCheckException(Runnable runnable, Class<? extends Throwable> expectedException) {
try {
runnable.run();
if (expectedException != null) {
throw new AssertionError("Didn't get expected exception " + expectedException.getSimpleName());
}
} catch (Throwable t) {
if (expectedException == null) {
throw new AssertionError("Got unexpected exception ", t);
}
if (!expectedException.isAssignableFrom(t.getClass())) {
throw new AssertionError(String.format("Got unexpected exception %s instead of %s",
t.getClass().getSimpleName(), expectedException.getSimpleName()), t);
}
}
}
that causes an extra (incorrect) exception wrapping, and also can cause false negative result (if expectedException == AssertionError.class)
public static void runAndCheckException(Runnable runnable, Class<? extends Throwable> expectedException) {
try {
runnable.run();
if (expectedException != null) {
throw new AssertionError("Didn't get expected exception " + expectedException.getSimpleName());
}
} catch (Throwable t) {
if (expectedException == null) {
throw new AssertionError("Got unexpected exception ", t);
}
if (!expectedException.isAssignableFrom(t.getClass())) {
throw new AssertionError(String.format("Got unexpected exception %s instead of %s",
t.getClass().getSimpleName(), expectedException.getSimpleName()), t);
}
}
}
- backported by
-
JDK-8142625 [TESTBUG] Utils.runAndCheckException doesn't work well if no exception thrown
- Resolved