Skip to content

Tighten preconditions that were too broad or too narrow - #1229

Merged
timtebeek merged 3 commits into
mainfrom
tim/narrow-broad-preconditions
Oct 25, 2026
Merged

timtebeek merged 3 commits into
mainfrom
tim/narrow-broad-preconditions

Conversation

@timtebeek

Copy link
Copy Markdown
Member

Several recipes were gated on something nearly every file of their kind uses, so their visitors ran far more often than needed, while KotlinTestMethodsShouldReturnUnit was gated so narrowly that TestNG Kotlin tests, which its annotation matcher accepts, were never visited.

Java recipes:

  • AdoptAssertJDurationAssertions now also requires a call on java.time.Duration (UsesMethod("java.time.Duration *(..)")), as both rewrites need the assertThat argument to be a method call on a Duration. The AssertJ isEqualTo/isGreaterThan/isLessThan check alone matched almost every AssertJ file.
  • RemoveIsMatcher now requires is(Matcher) as well as MatcherAssert.assertThat.
  • TestMethodsShouldBeVoid is gated on the four Jupiter annotations it actually checks, instead of org..*Test.
  • KotlinTestMethodsShouldReturnUnit now also accepts files using org.testng.annotations.Test. TestNG skips test methods that return a value by default, so the conversion helps there too. A new test covers a TestNG Kotlin test, and it fails without the precondition change.

Declarative recipes: the large relocation lists now sit behind an org.openrewrite.java.search.HasType precondition on the library's package glob, which covers every old type each list renames. A declarative precondition gates the whole recipe, build files included, so lists that were mixed with dependency changes moved into their own sub-recipe, at the same position in the parent:

  • Testcontainers2ContainerClasses was already pure Java, so it just gets org.testcontainers..* next to Singleton.
  • Wiremock3to4Migration: the ChangeType block moved into Wiremock4RelocatedTypes (com.github.tomakehurst.wiremock..*).
  • MigrateTruthToAssertJ: the Truth Java recipes and the ChangeMethodName/ChangeType list moved into TruthCodeToAssertJ (com.google.common.truth..*). The Java recipes had to move too: a precondition is evaluated where its recipe sits in the list, and TruthAssertWithMessageToAssertJ can remove the last explicit Truth type before the hasLength rename runs.
  • EasyMockToMockito: everything between the dependency changes and MockitoBestPractices moved into EasyMockCodeToMockito (org.easymock..*). MockitoBestPractices stays ungated, as before. I checked that a subclass of an EasyMockSupport subclass, with no EasyMock import, still passes the gate (supertypes count as types in use).

recipes.csv was regenerated with ./gradlew recipeCsvGenerate.

Verified with ./gradlew test on AdoptAssertJDurationAssertionsTest, AssertJBestPracticesTest, JUnit5BestPracticesTest and the hamcrest, cleanup, testcontainers, wiremock, truth and easymock packages: 645 tests, with one Truth failure that the sub-recipe split above fixed. After that change the YAML packages (186 tests) and KotlinTestMethodsShouldReturnUnitTest all pass.

Fixes item M2 of #1211.

…onditions-v1

# Conflicts:
#	src/main/resources/META-INF/rewrite/recipes.csv
…onditions-v1

# Conflicts:
#	src/main/java/org/openrewrite/java/testing/cleanup/TestMethodsShouldBeVoid.java
@timtebeek
timtebeek merged commit 0aeb74f into main Oct 25, 2026
2 checks passed
@timtebeek
timtebeek deleted the tim/narrow-broad-preconditions branch October 25, 2026 14:01
@github-project-automation github-project-automation Bot moved this from In Progress to Done in OpenRewrite Oct 25, 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.

1 participant