Add CleanupKotlinJUnit5AssertionImports recipe#893
Merged
jkschneider merged 8 commits intomainfrom Jan 26, 2026
Merged
Conversation
In Kotlin, when both `import org.junit.jupiter.api.*` and static imports from `org.junit.jupiter.api.Assertions` are present, the compiler cannot resolve which assertion method to use due to ambiguity between the Java static methods and the Kotlin extension functions. This recipe removes the Assertions imports when the wildcard import is present, allowing the Kotlin extension functions to be used instead. Fixes moderneinc/customer-requests#1711
src/main/java/org/openrewrite/java/testing/junit5/CleanupKotlinJUnit5AssertionImports.java
Outdated
Show resolved
Hide resolved
src/main/java/org/openrewrite/java/testing/junit5/CleanupKotlinJUnit5AssertionImports.java
Outdated
Show resolved
Hide resolved
src/main/java/org/openrewrite/java/testing/junit5/CleanupKotlinJUnit5AssertionImports.java
Outdated
Show resolved
Hide resolved
src/main/java/org/openrewrite/java/testing/junit5/CleanupKotlinJUnit5AssertionImports.java
Outdated
Show resolved
Hide resolved
src/test/java/org/openrewrite/java/testing/junit5/CleanupKotlinJUnit5AssertionImportsTest.java
Show resolved
Hide resolved
src/test/java/org/openrewrite/java/testing/junit5/CleanupKotlinJUnit5AssertionImportsTest.java
Outdated
Show resolved
Hide resolved
…nJUnit5AssertionImports.java Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
…nJUnit5AssertionImports.java Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
…nJUnit5AssertionImports.java Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
…nJUnit5AssertionImports.java Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
…nJUnit5AssertionImportsTest.java Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
…nJUnit5AssertionImportsTest.java Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
mergify bot
added a commit
to robfrank/linklift
that referenced
this pull request
Feb 14, 2026
…rom 3.26.0 to 3.27.0 [skip ci] Bumps [org.openrewrite.recipe:rewrite-testing-frameworks](https://github.com/openrewrite/rewrite-testing-frameworks) from 3.26.0 to 3.27.0. Release notes *Sourced from [org.openrewrite.recipe:rewrite-testing-frameworks's releases](https://github.com/openrewrite/rewrite-testing-frameworks/releases).* > 3.27.0 > ------ > > What's Changed > -------------- > > * Correcting MockWebServer migration for OkHttp by [`@steve-aom-elliott`](https://github.com/steve-aom-elliott) in [openrewrite/rewrite-testing-frameworks#849](https://redirect.github.com/openrewrite/rewrite-testing-frameworks/pull/849) > * Add `ReturnActual` recipe for AssertJ by [`@timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-testing-frameworks#891](https://redirect.github.com/openrewrite/rewrite-testing-frameworks/pull/891) > * Add `MockConstructionToTryWithResources` recipe by [`@jkschneider`](https://github.com/jkschneider) in [openrewrite/rewrite-testing-frameworks#892](https://redirect.github.com/openrewrite/rewrite-testing-frameworks/pull/892) > * Add CleanupKotlinJUnit5AssertionImports recipe by [`@jkschneider`](https://github.com/jkschneider) in [openrewrite/rewrite-testing-frameworks#893](https://redirect.github.com/openrewrite/rewrite-testing-frameworks/pull/893) > * Skip Kotlin files in PowerMockitoMockStaticToMockito by [`@jkschneider`](https://github.com/jkschneider) in [openrewrite/rewrite-testing-frameworks#894](https://redirect.github.com/openrewrite/rewrite-testing-frameworks/pull/894) > * Add test cases for `MockedConstruction` pattern by [`@jkschneider`](https://github.com/jkschneider) in [openrewrite/rewrite-testing-frameworks#895](https://redirect.github.com/openrewrite/rewrite-testing-frameworks/pull/895) > * Update byte buddy to a Java 25 compatible version by [`@Laurens-W`](https://github.com/Laurens-W) in [openrewrite/rewrite-testing-frameworks#897](https://redirect.github.com/openrewrite/rewrite-testing-frameworks/pull/897) > * Followup after the formatting merge by [`@Jenson3210`](https://github.com/Jenson3210) in [openrewrite/rewrite-testing-frameworks#900](https://redirect.github.com/openrewrite/rewrite-testing-frameworks/pull/900) > * Skip TemporaryFolder conversion when used in RuleChain. by [`@motlin`](https://github.com/motlin) in [openrewrite/rewrite-testing-frameworks#898](https://redirect.github.com/openrewrite/rewrite-testing-frameworks/pull/898) > * Add JUnit 4 to JUnit 5 exception class migrations. by [`@motlin`](https://github.com/motlin) in [openrewrite/rewrite-testing-frameworks#902](https://redirect.github.com/openrewrite/rewrite-testing-frameworks/pull/902) > * Make declarative recipes singletons by [`@sambsnyd`](https://github.com/sambsnyd) in [openrewrite/rewrite-testing-frameworks#905](https://redirect.github.com/openrewrite/rewrite-testing-frameworks/pull/905) > * Handle method references in JUnit Assert to Assertions migration. by [`@motlin`](https://github.com/motlin) in [openrewrite/rewrite-testing-frameworks#901](https://redirect.github.com/openrewrite/rewrite-testing-frameworks/pull/901) > * Add explicit test scope to AssertJ dependency additions by [`@Jenson3210`](https://github.com/Jenson3210) in [openrewrite/rewrite-testing-frameworks#903](https://redirect.github.com/openrewrite/rewrite-testing-frameworks/pull/903) > * Skip SimplifyChainedAssertJAssertion when actual type has wildcard type parameters by [`@knutwannheden`](https://github.com/knutwannheden) in [openrewrite/rewrite-testing-frameworks#906](https://redirect.github.com/openrewrite/rewrite-testing-frameworks/pull/906) > * Revert "Add explicit test scope to AssertJ dependency additions ([#903](https://redirect.github.com/openrewrite/rewrite-testing-frameworks/issues/903))" by [`@Jenson3210`](https://github.com/Jenson3210) in [openrewrite/rewrite-testing-frameworks#907](https://redirect.github.com/openrewrite/rewrite-testing-frameworks/pull/907) > * Fix AssertThrowsOnLastStatement to preserve parameterized types when extracting arguments. by [`@motlin`](https://github.com/motlin) in [openrewrite/rewrite-testing-frameworks#908](https://redirect.github.com/openrewrite/rewrite-testing-frameworks/pull/908) > * Enhance RemoveInitMocksIfRunnersSpecified to also remove openMocks by [`@dsgrieve`](https://github.com/dsgrieve) in [openrewrite/rewrite-testing-frameworks#904](https://redirect.github.com/openrewrite/rewrite-testing-frameworks/pull/904) > * `RemoveInitMocksIfRunnersSpecified` (or any recipe) should not have a mutable static field by [`@Jenson3210`](https://github.com/Jenson3210) in [openrewrite/rewrite-testing-frameworks#910](https://redirect.github.com/openrewrite/rewrite-testing-frameworks/pull/910) > > New Contributors > ---------------- > > * [`@motlin`](https://github.com/motlin) made their first contribution in [openrewrite/rewrite-testing-frameworks#898](https://redirect.github.com/openrewrite/rewrite-testing-frameworks/pull/898) > * [`@dsgrieve`](https://github.com/dsgrieve) made their first contribution in [openrewrite/rewrite-testing-frameworks#904](https://redirect.github.com/openrewrite/rewrite-testing-frameworks/pull/904) > > **Full Changelog**: <openrewrite/rewrite-testing-frameworks@v3.26.0...v3.27.0> Commits * [`41e6b42`](openrewrite/rewrite-testing-frameworks@41e6b42) Extract documentation examples from tests * [`403dd48`](openrewrite/rewrite-testing-frameworks@403dd48) review comment ([#910](https://redirect.github.com/openrewrite/rewrite-testing-frameworks/issues/910)) * [`e6c383b`](openrewrite/rewrite-testing-frameworks@e6c383b) Delete files * [`cf78c5a`](openrewrite/rewrite-testing-frameworks@cf78c5a) Enhance RemoveInitMocksIfRunnersSpecified to also remove openMocks ([#904](https://redirect.github.com/openrewrite/rewrite-testing-frameworks/issues/904)) * [`75c12cd`](openrewrite/rewrite-testing-frameworks@75c12cd) Fix AssertThrowsOnLastStatement to preserve parameterized types when extracti... * [`07b43cd`](openrewrite/rewrite-testing-frameworks@07b43cd) Revert "Add explicit test scope to AssertJ dependency additions ([#903](https://redirect.github.com/openrewrite/rewrite-testing-frameworks/issues/903))" ([#907](https://redirect.github.com/openrewrite/rewrite-testing-frameworks/issues/907)) * [`3a67bb9`](openrewrite/rewrite-testing-frameworks@3a67bb9) Skip SimplifyChainedAssertJAssertion when actual type has wildcard type param... * [`02eba0f`](openrewrite/rewrite-testing-frameworks@02eba0f) Add explicit test scope to AssertJ dependency additions ([#903](https://redirect.github.com/openrewrite/rewrite-testing-frameworks/issues/903)) * [`4e79908`](openrewrite/rewrite-testing-frameworks@4e79908) Handle method references in JUnit Assert to Assertions migration. ([#901](https://redirect.github.com/openrewrite/rewrite-testing-frameworks/issues/901)) * [`35409b0`](openrewrite/rewrite-testing-frameworks@35409b0) Make declarative recipes singletons ([#905](https://redirect.github.com/openrewrite/rewrite-testing-frameworks/issues/905)) * Additional commits viewable in [compare view](openrewrite/rewrite-testing-frameworks@v3.26.0...v3.27.0) [](https://docs.github.com/en/github/managing-security-vulnerabilities/about-dependabot-security-updates#about-compatibility-scores) Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting `@dependabot rebase`. [//]: # (dependabot-automerge-start) [//]: # (dependabot-automerge-end) --- Dependabot commands and options You can trigger Dependabot actions by commenting on this PR: - `@dependabot rebase` will rebase this PR - `@dependabot recreate` will recreate this PR, overwriting any edits that have been made to it - `@dependabot show ignore conditions` will show all of the ignore conditions of the specified dependency - `@dependabot ignore this major version` will close this PR and stop Dependabot creating any more for this major version (unless you reopen the PR or upgrade to it yourself) - `@dependabot ignore this minor version` will close this PR and stop Dependabot creating any more for this minor version (unless you reopen the PR or upgrade to it yourself) - `@dependabot ignore this dependency` will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
import org.junit.jupiter.api.*and static Assertions imports are present, remove the Assertions imports to resolve overload resolution ambiguityProblem
In Kotlin files, when both:
import org.junit.jupiter.api.*(imports Kotlin extension functions)import org.junit.jupiter.api.Assertions.*or specific Assertions importsare present, the Kotlin compiler cannot determine which
assertNull/assertNotNullto use due to ambiguity between:Assertions.assertNull(actual: Any!)assertNull(actual: Any?)Solution
This recipe detects Kotlin files with both imports present and removes the Assertions imports, allowing the Kotlin extension functions to be used. The Kotlin extensions have better nullability handling.
Test plan
Test with wildcard Assertions import (
import org.junit.jupiter.api.Assertions.*)Test with specific Assertions imports (
import org.junit.jupiter.api.Assertions.assertNull)Test no change when only API wildcard import is present
Test no change when only Assertions import is present
Test Java files are not affected
Fixes moderneinc/customer-requests#1711