Skip to content
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

removeAssertJRelatedElementsFromStackTrace does not remove all the JDK elements triggered by AssertJ #3449

Open
almondtools opened this issue Apr 29, 2024 · 3 comments
Assignees
Labels
type: bug A general bug
Milestone

Comments

@almondtools
Copy link
Contributor

almondtools commented Apr 29, 2024

If I call assertThat(0).isEqualTo(1);, I get an exception with a stacktrace

org.opentest4j.AssertionFailedError: 
expected: 1
 but was: 0
	at java.base/jdk.internal.reflect.DirectConstructorHandleAccessor.newInstance(DirectConstructorHandleAccessor.java:62)
	at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:502)
	at net.amygdalum.WorldTest.testStacktrace(WorldTest.java:13)
	at ...

The first two lines are superfluous and not understandable (WorldTest:13 does not call newInstanceWithCaller). I think that some bug fix removing assertj internals from the stack now does not remove these lines any more.

This bug seems so obvious to me, that I am uncertain whether my configuration is the reason, so I added my IDE and my build system to the context below. Feel free to ask more, if you have a suspect.

  • assertj core version: 3.25.3
  • java version: 21
  • test framework version: Junit Jupiter
  • os (if relevant): Windows
  • build: grade
  • IDE: Eclipse

Test case reproducing the bug

class WorldTest {

	@Test
	void testStacktrace() {
		assertThat(0).isEqualTo(1);
	}
}
@scordio
Copy link
Member

scordio commented Apr 29, 2024

Good catch, @almondtools!

I would also consider the bug quite obvious but yours seems to be the first feedback on a code that never changed since the forking from Fest Assert (66f11f8) 🙂

According to the Javadoc example of removeAssertJRelatedElementsFromStackTrace, pruning all the stack trace elements triggered by AssertJ was never intended. Only the one right before the first AssertJ method is supposed to be removed.

If you would run the following:

@Test
void testStacktrace() {
	Assertions.setRemoveAssertJRelatedElementsFromStackTrace(false);
	assertThat(0).isEqualTo(1);
}

you would see how the stack trace really looks like:

...
kept -->	at java.base/jdk.internal.reflect.DirectConstructorHandleAccessor.newInstance(DirectConstructorHandleAccessor.java:62)
kept -->	at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:502)
removed -->	at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:486)
removed -->	at org.assertj.core.error.ConstructorInvoker.newInstance(ConstructorInvoker.java:28)
removed -->	at org.assertj.core.error.ShouldBeEqual.assertionFailedError(ShouldBeEqual.java:223)
...

I believe any element that is a consequence of org.assertj.core.error.ConstructorInvoker.newInstance is not valuable for the users and should be pruned.

Referring to the original example, what users should see is only:

org.opentest4j.AssertionFailedError: 
expected: 1
 but was: 0
	at net.amygdalum.WorldTest.testStacktrace(WorldTest.java:13)
	at ...

@joel-costigliola how do you see it?

@scordio scordio added the type: bug A general bug label Apr 29, 2024
@scordio scordio changed the title Stacktrace for "isEqualTo" errors contains too many lines removeAssertJRelatedElementsFromStackTrace does not remove all the JDK elements triggered by AssertJ Apr 29, 2024
@joel-costigliola
Copy link
Member

joel-costigliola commented Apr 29, 2024

I completely agree to remove the stack before the user's code as the aim is to clear the clutter from the stack.

Let's do it for 3.26.0

@joel-costigliola joel-costigliola added this to the 3.26.0 milestone Apr 29, 2024
@scordio scordio self-assigned this Apr 30, 2024
@almondtools
Copy link
Contributor Author

Also check to keep the handling of this code consistent

	@Test
	void testStacktrace2() {
		assertThat(0)
			.satisfies(x -> assertThat(x).isEqualTo(1));
	}

Here the superfluous lines from the example above appear in the middle of the stack trace. I have no preference how to handle this, but just wanted to point out that this test is similar but different.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug A general bug
Projects
None yet
Development

No branches or pull requests

3 participants