-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Trim internal frames from AssertionFailedError #5159
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
a04dd02 to
94360c1
Compare
junit-jupiter-api/src/main/java/org/junit/jupiter/api/AssertionFailureBuilder.java
Show resolved
Hide resolved
When using `JUnit.start` and creating a failing test, users will be confronted with a large stacktrace with mostly irrelevant information. Even after #5158 is merged, the stacktrace will contain several internal frames: ``` org.opentest4j.AssertionFailedError: expected: <11> but was: <12> at [email protected]/org.junit.jupiter.api.AssertionFailureBuilder.build(AssertionFailureBuilder.java:158) at [email protected]/org.junit.jupiter.api.AssertionFailureBuilder.buildAndThrow(AssertionFailureBuilder.java:139) at [email protected]/org.junit.jupiter.api.AssertEquals.failNotEqual(AssertEquals.java:201) at [email protected]/org.junit.jupiter.api.AssertEquals.assertEquals(AssertEquals.java:152) at [email protected]/org.junit.jupiter.api.AssertEquals.assertEquals(AssertEquals.java:147) at [email protected]/org.junit.jupiter.api.Assertions.assertEquals(Assertions.java:558) at com.examp.project/com.example.project.HelloTest.stringLength(HelloTest.java:14) ``` By pruning these internal frames, the stacktrace can be reduced to a much more readable: ``` org.opentest4j.AssertionFailedError: expected: <11> but was: <12> at com.examp.project/com.example.project.HelloTest.stringLength(HelloTest.java:14) ``` Comparable behaviour can be found in AssertJ[1] and IDEA which folds internal frames in the console using `<6 internal line>`. The pruning functionality is intentionally added to the `AssertionFailureBuilder` rather than the `ExceptionUtils` to enable other users of the builder to also prune the internal frames from their own assertions. 1. https://github.com/assertj/assertj/blob/79bdebf1817692e5e0ff5ee3ab097dcd104d47ae/assertj-core/src/main/java/org/assertj/core/util/Throwables.java#L117-L148
94360c1 to
9e04b51
Compare
platform-tests/src/test/java/org/junit/platform/StackTracePruningTests.java
Show resolved
Hide resolved
|
I think we should preserve the bottommost |
To me, the extra frame makes it explicit that a specific assertion method failed. Let's discuss with the rest of the team, though. |
| * @return this builder for method chaining | ||
| */ | ||
| @API(status = EXPERIMENTAL, since = "6.1") | ||
| public AssertionFailureBuilder trimStacktrace(@Nullable Class<?> target, int retain) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it make sense to overload this with a "pass 1" variant?
public AssertionFailureBuilder trimStacktrace(@Nullable Class<?> target) {
return trimStacktrace(target, 1);
}There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've thought about that. But the "natural" default value seems to be 0. We are in most places keeping one extra. So though duplicated quite often (but not always), I think we should keep that.
On reflection I do have my doubts about putting both parameters in a single method though. For a builder, this really should be two separate methods.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On reflection I do have my doubts about putting both parameters in a single method though. For a builder, this really should be two separate methods.
It does force the design towards using a strategy pattern but I don't think we have enough design constraints for that yet. So I've just extracted a new method for the second argument and set the default value to 1.
sormuras
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me, added a tiny question regarding an overload for the common case of retain 1 frame.

When using
JUnit.startand creating a failing test, users will be confronted with a large stacktrace with mostly irrelevant information. Even after #5158 is merged, the stacktrace will contain several internal frames:By pruning these internal frames, the stacktrace can be reduced to a much more readable:
The pruning functionality is intentionally added to the
AssertionFailureBuilderrather than theExceptionUtilsto enable other users to also prune the internal frames from their own assertions.I hereby agree to the terms of the JUnit Contributor License Agreement.
Definition of Done
@APIannotations