Skip to content

Track Hamcrest not(..) negation locally instead of on the ExecutionContext - #1214

Merged
timtebeek merged 1 commit into
mainfrom
tim/hamcrest-not-matcher-local-negation
Oct 24, 2026
Merged

timtebeek merged 1 commit into
mainfrom
tim/hamcrest-not-matcher-local-negation

Conversation

@timtebeek

Copy link
Copy Markdown
Member

HamcrestMatcherToJUnit5 remembered whether an assertion was negated on the ExecutionContext, keyed by the printed matcher, so a not(..) in one assertion flipped every later assertion with the same matcher text:

assertThat(a, not(nullValue()));
assertThat(b, nullValue());

became assertNotNull(a); assertNotNull(b);. The same state was shared with HamcrestInstanceOfToJUnit5, and not(not(x)) failed with a NullPointerException.

Both recipes now count the not(..) layers they unwrap, so the result above is assertNotNull(a); assertNull(b); and not(not(nullValue())) becomes assertNull(..). The hamcrest tests no longer turn off the immutable execution context check, so RewriteTest guards against this coming back.

Fixes item E1 of #1211.

…nContext`

`RemoveNotMatcherVisitor` stored whether a matcher was negated on the `ExecutionContext`, keyed by the printed matcher, so a negation leaked into later assertions with the same matcher text: `assertThat(a, not(nullValue())); assertThat(b, nullValue());` became `assertNotNull(a); assertNotNull(b);`, and `not(not(x))` threw a `NullPointerException`. `HamcrestMatcherToJUnit5` and `HamcrestInstanceOfToJUnit5` now flip the negation for each `not(..)` layer they unwrap, and the hamcrest tests no longer disable the immutable execution context check that would have caught this.
@github-project-automation github-project-automation Bot moved this to In Progress in OpenRewrite Oct 24, 2026
@timtebeek
timtebeek merged commit ad8bea1 into main Oct 24, 2026
2 checks passed
@timtebeek
timtebeek deleted the tim/hamcrest-not-matcher-local-negation branch October 24, 2026 11:17
@github-project-automation github-project-automation Bot moved this from In Progress to Done in OpenRewrite Oct 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

1 participant