Skip to content

Declare what templates name and build trees as the parser does - #1197

Merged
jkschneider merged 4 commits into
mainfrom
rewrite-run-checks-2
Oct 18, 2026
Merged

jkschneider merged 4 commits into
mainfrom
rewrite-run-checks-2

Conversation

@jkschneider

Copy link
Copy Markdown
Member

Several recipes wrote templates that name a type or static member without declaring it, so the name resolved only through the imports of the file the template was applied to. The AssertJ templates wrote assertThat(..) that way. On code that calls Assertions.assertThat(..) qualified, they made an untyped call and added no import. They now declare the assertThat of the call they replace, read from its method type, and the output imports it. The PowerMock migrations declare and import the mocked class they name. AssertTrueEqualsToAssertEquals declares org.junit.jupiter.api.Assertions where it declared a method as a type.

Two recipes built trees that their parser doesn't make of the text they print. ArgumentMatcherToLambda's lambda parameter is now a declaration without a type, not an identifier. MigrateJUnitTestCase removes setName(..) and keeps its text as a comment. It used to hide the call behind an empty //, and that hidden call folded the static imports of one test into *. That test and its example now import two methods by name.

New rewriteRun checks in rewrite-test fail tests on both problems. Four tests are added for fully qualified assertThat calls.

The templates of SimplifyChainedAssertJAssertion, SimplifyArrayLengthAssertion, AdoptAssertJDurationAssertions and SimplifyHasSizeFromIsEqualToAssertion wrote `assertThat(..)` and resolved it only through the static import of the file they were applied to. On code that calls `Assertions.assertThat(..)` qualified, the call they wrote had no type and the output didn't import it. Each template now declares the `assertThat` of the call it replaces, read from that call's method type, and the output imports it. SimplifyChainedAssertJAssertion also declares `java.nio.file.Path` where it writes `Path.of(..)`.

A new rewriteRun check in rewrite-test fails tests on templates that name what they don't declare. Four tests are added for fully qualified `assertThat` calls.
For a qualified `Assertions.assertTrue(..)`, the template declared `org.junit.jupiter.api.Assertions.assertEquals` as a type import, so the `Assertions` it writes resolved only through the file's own import. It now declares `org.junit.jupiter.api.Assertions`, and the output imports it where the call was fully qualified.

A new rewriteRun check in rewrite-test fails tests on templates that name what they don't declare.
The field ScopedMocks declares, such as `MockedStatic<Calendar>`, and the `when`, `verify` and dynamic `when` calls PowerMockitoMockStaticToMockito writes, such as `Calendar::getInstance`, name the mocked class by its class name. Their templates resolved it only through the imports of the test they were applied to. They now declare the class's top-level type by its fully qualified name, unless it is in the default package, `java.lang` or the test's package. The field and the dynamic `when` also import it, so a mocked class written fully qualified still works.

A new rewriteRun check in rewrite-test fails tests on templates that name what they don't declare.
@github-project-automation github-project-automation Bot moved this to In Progress in OpenRewrite Oct 18, 2026
@jkschneider
jkschneider merged commit b5e82cc into main Oct 18, 2026
2 checks passed
@jkschneider
jkschneider deleted the rewrite-run-checks-2 branch October 18, 2026 04:00
@github-project-automation github-project-automation Bot moved this from In Progress to Done in OpenRewrite Oct 18, 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