Skip to content

HamcrestMatcherToJUnit5: support Kotlin sources#1058

Merged
timtebeek merged 6 commits into
mainfrom
tim/kotlin-hamcrest-to-junit5
Jul 25, 2026
Merged

HamcrestMatcherToJUnit5: support Kotlin sources#1058
timtebeek merged 6 commits into
mainfrom
tim/kotlin-hamcrest-to-junit5

Conversation

@timtebeek

@timtebeek timtebeek commented Jul 23, 2026

Copy link
Copy Markdown
Member

HamcrestMatcherToJUnit5 threw "No JavaSourceFile parent found" on Kotlin test sources because it always used JavaTemplate; it now branches on K.CompilationUnit and applies a KotlinTemplate (via KotlinParser) for Kotlin sources. It uses a Kotlin-specific template variant where the Java form is invalid Kotlin (Collection.size().size) and fixes a latent unbalanced-paren typo in the containsString template that only the Kotlin parser rejected. Type attribution of the generated assertions relies on the upstream fix openrewrite/rewrite#8324 (KotlinTemplate attributes members selected on substituted placeholders), so it needs a rewrite snapshot ≥ 8.88.0 build 59. The only remaining manual attribution is the enclosing assertX(condition, reason) call, which Kotlin leaves untyped because JUnit's (…, String) and (…, Supplier) overloads are ambiguous when the reason is a substituted placeholder. Adds Kotlin tests covering equalTo, not, greaterThan, containsString, nullValue, collections, and reason arguments.

@github-project-automation github-project-automation Bot moved this to In Progress in OpenRewrite Jul 23, 2026
@timtebeek
timtebeek marked this pull request as draft July 24, 2026 10:24
@timtebeek timtebeek self-assigned this Jul 24, 2026
Comment thread src/main/java/org/openrewrite/java/testing/hamcrest/HamcrestMatcherToJUnit5.java Outdated
rewrite#8324 (KotlinTemplate: attribute members selected on substituted
placeholders) attributes the synthesized member calls, which cascades to
the enclosing assertion, so the manual attributeTypes pass is no longer
needed.
rewrite#8324 types members-on-placeholders and their enclosing calls, but
JUnit's assertX(condition, String) vs assertX(condition, Supplier) overloads
stay ambiguous when the reason is a substituted placeholder, leaving the call
untyped so the static import is dropped. Attribute just that enclosing call.
@timtebeek
timtebeek marked this pull request as ready for review July 25, 2026 07:04
@timtebeek timtebeek changed the title HamcrestMatcherToJUnit5: support Kotlin via KotlinTemplate branches HamcrestMatcherToJUnit5: support Kotlin sources Jul 25, 2026
@timtebeek
timtebeek merged commit b1bbe20 into main Jul 25, 2026
1 check passed
@github-project-automation github-project-automation Bot moved this from In Progress to Done in OpenRewrite Jul 25, 2026
@timtebeek
timtebeek deleted the tim/kotlin-hamcrest-to-junit5 branch July 25, 2026 07:06
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