rewrite-testing-frameworks icon indicating copy to clipboard operation
rewrite-testing-frameworks copied to clipboard

AssertJ recipes should not change `assertThat(null== a).isEqualTo(true)` to `assertThat(null).isSameAs(a)`

Open ChenyuWang98 opened this issue 2 months ago • 5 comments

Compilation error

assertThat(null== a).isEqualTo(true) => assertThat(null).isSameAs(a);

Better

assertThat(null== a).isEqualTo(true) => assertThat(a).isNull()

ChenyuWang98 avatar Dec 05 '25 08:12 ChenyuWang98

hi @ChenyuWang98 ; It looks like both the left and right side here are using AssertJ already, is that correct? What was the original in Hamcrest before any migration?

Or would you want to see the left hand side optimized towards the right hand alternative when already using AssertJ?

timtebeek avatar Dec 05 '25 09:12 timtebeek

hi @ChenyuWang98 ; It looks like both the left and right side here are using AssertJ already, is that correct? What was the original in Hamcrest before any migration?

Or would you want to see the left hand side optimized towards the right hand alternative when already using AssertJ?

My problem is that when the first line using assertJ is updated again using this recipe, it becomes the statement on the right side of the first line. This causes a syntax problem, and I think it should be changed to the second line or left unchanged.

ChenyuWang98 avatar Dec 09 '25 00:12 ChenyuWang98

Thanks for the clarification; that helps.

timtebeek avatar Dec 09 '25 11:12 timtebeek

It looks like this might be coming from the order in which the recipes generated from these lines are processed: https://github.com/PicnicSupermarket/error-prone-support/blob/6d7d9a4b77621ef3bc2719a56f443cb713d6dce3/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/AssertJObjectRules.java#L98-L163

timtebeek avatar Dec 09 '25 12:12 timtebeek

Probably not helpful in the OpenRewrite context, but in case you're wondering why this works with Refaster: only because we implemented a custom heuristic.

Stephan202 avatar Dec 09 '25 20:12 Stephan202