Skip to content

Fix dangling constructor calls when migrating JUnit TestCase - #1119

Closed
manduinca wants to merge 9 commits into
openrewrite:mainfrom
manduinca:fix/junit-testcase-constructor-calls
Closed

manduinca wants to merge 9 commits into
openrewrite:mainfrom
manduinca:fix/junit-testcase-constructor-calls

Conversation

@manduinca

@manduinca manduinca commented Sep 11, 2026 •

Copy link
Copy Markdown
Contributor

What

Fixes #1107.

I track redundant TestCase constructors across source files, update simple constructor calls to the no-argument form, and propagate removal through subclass constructors. I preserve constructors where dropping arguments would discard side effects or invalidate constructor references.

Details

  • I scan constructor declarations and references across all source files before editing.
  • I remove empty and forwarding constructors to a fixed point, including super(...) and this(...) chains, and update matching new expressions.
  • I use stable signatures because migration changes the declaring type's hierarchy between cycles.
  • I preserve constructors with unsafe argument evaluations (including field reads), member references, annotations, declared exceptions, or calls from classes overriding TestCase#run(TestResult). I also preserve overload sets when another constructor must remain.
  • I retain constructors with additional statements; moving their initialization to Jupiter lifecycle methods remains the separate scope of A retained TestCase(String) constructor leaves the migrated test class unrunnable under JUnit 5 #1108.
  • I kept the setName migration and existing TestCase safeguards from the earlier main merge (bc860019, October 23).

Validation

I fixed the new accumulator validation failure by recording a small constructor summary during scanning instead of retaining J.MethodDeclaration trees. The summary keeps only the declaring type name, a body-safety flag, and an optional delegated-constructor signature.

I reran all 26 MigrateJUnitTestCaseTest tests successfully. My related selection completed 64 tests: 62 passed, and the two Cucumber failures have the same names and failure messages as the comparison against main bc860019 recorded on October 23. This is a comparison with that earlier baseline, not a new run against main 4506516b. I also ran check -x test; license checks and recipe CSV validation passed.

I reproduced the accumulator rejection in an external harness using the current official AccumulatorContents, Tree, and Heavyweight sources without changes. Three scenarios fail on the previously published implementation (ed131437) with the same retained J.MethodDeclaration path reported by Codegenome and pass with this fix. The scenarios cover a simple constructor, a cross-file hierarchy, and a constructor retained because its arguments have side effects. The harness checks the accumulator after scanning and after editing.

The harness runs on the public Rewrite 8.90.4 runtime with an external compatibility overlay for the official Tree/Heavyweight marker and the two required helper methods extracted from the official sources. This is a focused reproduction of the accumulator validation, not the exact current Codegenome environment or a run of its full 2,285-test suite. The harness and overlay are outside the PR diff, and I have not disabled any validators.

The standard ./gradlew build still stops during configuration because it cannot resolve org.openrewrite.build.recipe-library:latest.release through the declared repositories. For local validation I used Gradle 9.8.2, JDK 21, and the existing external, uncommitted init script selecting public build plugin 2.23.1, Rewrite 8.90.4, java-dependencies 1.60.2, static-analysis 2.41.1, error_prone_core 2.50.0, and cucumber-jvm 2.15.0. I have not changed the repository build configuration or test sources. The subsequent Codegenome build completed successfully (BUILD SUCCESSFUL in 8m 27s) for this commit merged into main c13714ff.

@manduinca
manduinca marked this pull request as ready for review September 11, 2026 08:13
@greg-at-moderne
greg-at-moderne self-requested a review September 17, 2026 11:45
greg-at-moderne and others added 5 commits September 17, 2026 14:11
# Conflicts:
#	src/main/java/org/openrewrite/java/testing/junit5/MigrateJUnitTestCase.java
I keep constructor-call handling together with main's setName migration and its existing TestCase safeguards.
@manduinca

Copy link
Copy Markdown
Contributor Author

I've resolved the merge conflict, and all 26 focused recipe tests pass locally. The Java 25 CI job stops before compilation because downloading rewrite-build-gradle-plugin 2.27.4 from Codegenome returns HTTP 401. Could you check the project's artifact access or CI configuration and let me know if there is anything I need to adjust?

@github-project-automation github-project-automation Bot moved this from In Progress to Done in OpenRewrite Nov 17, 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.

MigrateJUnitTestCase removes the TestCase(String) constructor without updating its callers, breaking compilation

2 participants