Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8137020

[TESTBUG] Utils.runAndCheckException doesn't work well if no exception thrown

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P3 P3
    • 9
    • 9
    • hotspot
    • b89

        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);
                    }
                }
            }

              kzhaldyb Kirill Zhaldybin (Inactive)
              iignatyev Igor Ignatyev (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

                Created:
                Updated:
                Resolved: