-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Description
ErrorCollector and MultipleFailureException are incredibly useful tools, but feel only half integrated into Junit. ExternalResource and TestWatcher both execute their functionality even if the test throws an exception, and if that functionality itself throws, then these accumulate the multiple exceptions into a MultipleFailureException. However, Verifier (and thus, almost all rules that extend it), does NOT run if the test throws a MultipleFailureException, which is a missed opportunity. This is especially ironic since ErrorCollector itself extends Verifier, so if a test has multiple ErrorCollector rules for any reason (inheritance or toolchain RuleChains), then only the innermost ErrorCollector collects errors. ErrorCollector and Verifier ought to work nicely together.
Verifier ought to wrap its base.evaluate();
call in a try/catch, and if it catches a MultipleFailureException, then it ought to execute .verify(). It should accumulate the MultipleFailureException exceptions and any new exception(s?) thrown by .verify(), and throw those as a new MultipleFailureException.
We could consider catching all exceptions and running .verify() and rethrowing the original exception afterward, but for tests failing with non-MultipleFailureException, running post-test-verification almost certainly doesn't make sense. It would virtually always fail, providing noise instead of value, so I do not propose executing it for any exception other than MultipleFailureException. It is worth noting that this reasoning only really applies to exceptions thrown from the test method itself, so ideally we do want to run post-test-verification if inner Rules' tear-downs throw exceptions, but usefully, existing JUnit rules only throw MultipleFailureException in teardown, so no additional work is needed.
I haven't yet examined the junit-5 code to see (A) this has already been fixed, (B) 5 has new class/method names, or (C) if 5 rules now throw exceptions besides MultipleFailureException in teardown. I understand and accept that no new junit-4 releases are planned, and that if this feature is accepted, then the changes will probably only be implemented in junit-5.