Skip to content

Comments

Add .contextSensitive() to Hamcrest conversion templates and improve type matching in CollapseConsecutiveAssertThatStatements#912

Merged
timtebeek merged 3 commits intomainfrom
timtebeek/hamcrest-type-info
Feb 21, 2026
Merged

Add .contextSensitive() to Hamcrest conversion templates and improve type matching in CollapseConsecutiveAssertThatStatements#912
timtebeek merged 3 commits intomainfrom
timtebeek/hamcrest-type-info

Conversation

@timtebeek
Copy link
Member

  • Add .contextSensitive() to JavaTemplate builders in HamcrestMatcherToAssertJ, HamcrestNotMatcherToAssertJ, and HamcrestIsMatcherToAssertJ to ensure templates are compiled in proper source context
  • Improve CollapseConsecutiveAssertThatStatements with fallback for partially resolved SELF return types (e.g., when assertion methods return AbstractIterableAssert instead of ListAssert)
  • Update test expectations: isNotNull() now collapses with other assertions and is removed by SimplifyRedundantAssertJChains
  • Add test case for custom types to verify hamcrest-to-AssertJ conversion works

Fixes the issue where the AssertJ best practices recipe required multiple runs on Hamcrest test files before seeing chained assertions.

…type matching in CollapseConsecutiveAssertThatStatements

- Add .contextSensitive() to JavaTemplate builders in HamcrestMatcherToAssertJ, HamcrestNotMatcherToAssertJ, and HamcrestIsMatcherToAssertJ to ensure templates are compiled in proper source context
- Improve CollapseConsecutiveAssertThatStatements with fallback for partially resolved SELF return types (e.g., when assertion methods return AbstractIterableAssert instead of ListAssert)
- Update test expectations: isNotNull() now collapses with other assertions and is removed by SimplifyRedundantAssertJChains
- Add test case for custom types to verify hamcrest-to-AssertJ conversion works

Fixes the issue where the AssertJ best practices recipe required multiple runs on Hamcrest test files before seeing chained assertions.
Comment on lines 188 to 190
assertThat(biscuits).hasSize(3);
assertThat(biscuits).contains(new Biscuit("Chocolate"));
assertThat(biscuits).doesNotContain(new Biscuit("Raisin"));
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally these are collapsed as well.

@timtebeek timtebeek marked this pull request as draft February 21, 2026 12:42
Use #{any(java.util.List)} instead of #{any()} for Java standard library
types in HamcrestMatcherToAssertJ.typeToIndicator(). This gives the
template parser enough type information to resolve the correct assertThat
overload, producing proper return types even when generic type parameters
reference custom types not on the template parser classpath.

This enables CollapseConsecutiveAssertThatStatements to chain assertions
on custom types (like List<Biscuit>) in a single pass, no longer
requiring TypeValidation.none() in tests.
@timtebeek timtebeek marked this pull request as ready for review February 21, 2026 14:44
@timtebeek timtebeek merged commit c270c74 into main Feb 21, 2026
1 check passed
@timtebeek timtebeek deleted the timtebeek/hamcrest-type-info branch February 21, 2026 14:44
@github-project-automation github-project-automation bot moved this from In Progress to Done in OpenRewrite Feb 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

1 participant