Skip to content

Remove now-invalid @Override when migrating TestCase subclasses - #1104

Merged
timtebeek merged 4 commits into
mainfrom
tim/issue-1103
Aug 24, 2026
Merged

Remove now-invalid @Override when migrating TestCase subclasses#1104
timtebeek merged 4 commits into
mainfrom
tim/issue-1103

Conversation

@timtebeek

@timtebeek timtebeek commented Aug 24, 2026

Copy link
Copy Markdown
Member

MigrateJUnitTestCase only removed @Override as a side effect of adding a Jupiter lifecycle annotation. When a TestCase subclass had already been half-migrated to JUnit 4 — setUp() carrying both @Override and @Before — that branch was skipped, so extends TestCase was removed while the @Override stayed behind and the class no longer compiled. Same for getName() and countTestCases(), which never hit that branch at all.

Every method declaration in a migrated TestCase subclass is now checked against the supertypes it will still have after migration, skipping the junit.framework types that are going away. @Override is only removed when nothing else declares a matching method, so these keep it:

  • toString(), equals(), hashCode() (declared on Object, and on TestCase only incidentally)
  • implementations of an interface the class declares
  • overrides of an intermediate base class that itself declares the method

RemoveAnnotationVisitor handles the removal so the leftover blank line is cleaned up; the body is detached first so @Override on methods of anonymous classes inside is untouched.

One existing expectation changed: in convertExtendedTestCase, MathTest.setUp() overrides CTest.setUp(), which CTest declares itself, so the @Override is now retained rather than stripped.

`MigrateJUnitTestCase` only dropped `@Override` when it added a Jupiter
lifecycle annotation, so `setUp()`/`tearDown()` that already carried
`@Before`/`@After` kept an `@Override` that no longer compiles once
`extends TestCase` is removed, as did `getName()` and `countTestCases()`.

Now every method in a migrated `TestCase` subclass is checked against its
remaining supertypes, skipping the `junit.framework` types that are going
away, so `toString()`, interface implementations and overrides of an
intermediate base that declares the method keep their annotation.

Fixes #1103
@timtebeek
timtebeek merged commit 08fd021 into main Aug 24, 2026
1 check passed
@timtebeek
timtebeek deleted the tim/issue-1103 branch August 24, 2026 21:51
@github-project-automation github-project-automation Bot moved this from In Progress to Done in OpenRewrite Aug 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.

JUnit 4 to 5 migration leaves @Override on methods that overrode TestCase, breaking compilation

1 participant