Skip to content

Fix JUnit4to5Migration for classes that extend org.junit.Assert#996

Merged
timtebeek merged 3 commits into
openrewrite:mainfrom
motlin:extends-Assert
May 15, 2026
Merged

Fix JUnit4to5Migration for classes that extend org.junit.Assert#996
timtebeek merged 3 commits into
openrewrite:mainfrom
motlin:extends-Assert

Conversation

@motlin
Copy link
Copy Markdown
Contributor

@motlin motlin commented May 9, 2026

The JUnit 4 to 5 migration produces broken code when a class extends org.junit.Assert and uses unqualified inherited assertion methods. The JUnit4to5Migration aggregator:

  • adds Jupiter static imports for assertEquals / assertTrue,
  • reorders the message argument from JUnit 4 position (first) to Jupiter position (last),
  • but leaves import org.junit.Assert and extends Assert unchanged.

Because inherited methods shadow static imports in Java, the reordered call assertEquals(1, 1, "expected message") resolves against JUnit 4's Assert.assertEquals, which has no (int, int, String) overload, so the migrated code will not compile.

I'm not sure what the proper fix should be, so I'm just submitting a test that demonstrates the broken output. The after block reflects what the recipes actually produce today, so the test passes and CI stays green. Happy to help with the fix, but I don't have a clear idea of what it ought to be.

@github-project-automation github-project-automation Bot moved this to In Progress in OpenRewrite May 9, 2026
@timtebeek timtebeek self-requested a review May 10, 2026 10:21
@timtebeek timtebeek marked this pull request as draft May 10, 2026 10:21
@motlin motlin marked this pull request as ready for review May 10, 2026 14:29
@timtebeek
Copy link
Copy Markdown
Member

Thanks @motlin .. I'd marked this as draft to signify it should not be merged as is just yet. Instead we'll look to fix the recipe before merging.

@timtebeek timtebeek marked this pull request as draft May 10, 2026 18:02
When a class extends `org.junit.Assert`, unqualified inherited
assertion calls otherwise shadow the Jupiter static imports and
keep resolving to the JUnit 4 overloads, producing code that fails
to compile.
@timtebeek timtebeek marked this pull request as ready for review May 15, 2026 16:48
@timtebeek timtebeek merged commit 28b439c into openrewrite:main May 15, 2026
1 check passed
@github-project-automation github-project-automation Bot moved this from In Progress to Done in OpenRewrite May 15, 2026
@motlin motlin deleted the extends-Assert branch May 19, 2026 12:52
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.

2 participants