ota4j-team / opentest4j

Open Test Alliance for the JVM
Apache License 2.0
284 stars 37 forks source link

Cannot attach cause to AssertionFailedError even when it has none #70

Closed TWiStErRob closed 1 year ago

TWiStErRob commented 1 year ago

(Forked from https://github.com/ota4j-team/opentest4j/issues/5#issuecomment-940474063)

The fix (https://github.com/ota4j-team/opentest4j/commit/90db8b4ff81b737b82155880ded7ddff2eb62b0e) for #5 has a side effect that it's not possible to attach a cause on top of the AssertionFailedError.

This used to work:

generateSequence(ex) { it.cause }.last().initCause(IllegalArgumentException("Command: $command"))

and the benefit is adding additional information to validation/assertion done by other frameworks. There's always a root cause that has no cause (null), so calling initCause on these root cause exceptions should be possible.

Calling initCause on the root cause would work, if AssertionFailedError wasn't calling super(message, null) or initCause in the constructor. Passing null to super locks in the cause as null without the ability to modify it later.

This project still declars Java 6 as minimum. But this could be remedied with if (cause != null) initCause(cause).

It was possible to work around this by setting the cause reflectively, but this is prohibited now on later Java version (16+).

TWiStErRob commented 1 year ago

... and I just noticed that there's already a fix for this in https://github.com/ota4j-team/opentest4j/pull/19