Skip to content

Add PowerMockitoDoStubbingToMockito recipe#948

Merged
timtebeek merged 3 commits intomainfrom
bmuschko/fix-donothing-when
Mar 30, 2026
Merged

Add PowerMockitoDoStubbingToMockito recipe#948
timtebeek merged 3 commits intomainfrom
bmuschko/fix-donothing-when

Conversation

@bmuschko
Copy link
Copy Markdown
Contributor

@bmuschko bmuschko commented Mar 29, 2026

Summary

After running Mockito1to3Migration, PowerMockito's string-based private method stubbing pattern doX().when(instance, "methodName", args...) is incorrectly translated. The recipe only changes the class target from PowerMockito to Mockito but does not restructure the .when() call, resulting in code that doesn't compile:

Expected 1 argument but found 2

This happens because PowerMockitoStubber.when(T, String, Object...) has a multi-arg overload for private method stubbing, while Mockito's Stubber.when(T) only accepts a single argument.

This PR adds a PowerMockitoDoStubbingToMockito recipe that rewrites the string-based form into Mockito's chained form as part of ReplacePowerMockito.

Use cases covered

Before (PowerMockito) After (Mockito) Status
doNothing().when(spy, "voidMethod") doNothing().when(spy).voidMethod() Converted
doReturn(val).when(spy, "method") doReturn(val).when(spy).method() Converted
doThrow(ex).when(spy, "method") doThrow(ex).when(spy).method() Converted
doX().when(spy, "method", arg1, arg2) doX().when(spy).method(arg1, arg2) Converted
doX().when(Foo.class, "staticMethod") Left unchanged Skipped (static variant, handled separately)
doX().when(mock).method() (already chained) Left unchanged Skipped (no string method name)

Type information handling

The recipe resolves method types from the instance's type hierarchy so the output AST has proper type attribution. Specifically:

  • The intermediate when(instance) call gets its return type updated from void to the instance type (matching Mockito's Stubber.when(T) → T)
  • The chained method name (e.g., .clear()) gets its JavaType.Method resolved by walking the target type's declared methods and supertypes

Note on typeValidationOptions(identifiers=false) in tests

The test class uses .typeValidationOptions(TypeValidation.builder().identifiers(false).build()). This is not caused by the recipe — it's a pre-existing limitation of the PowerMock classpath stubs used in tests. The Calendar identifier in Calendar.getInstance() (which the recipe does not touch) lacks full type attribution when parsed with the PowerMock test classpath. This was confirmed by using afterTypeValidationOptions (output-only validation), which showed the same failure on the unmodified input AST. The same pattern is used by MockitoWhenOnStaticToMockStaticTest and CloseUnclosedStaticMocksTest.

References

Rewrite PowerMockito's string-based private method stubbing into
standard Mockito chained form so migrated code compiles.
@github-project-automation github-project-automation Bot moved this to In Progress in OpenRewrite Mar 29, 2026
@bmuschko bmuschko added the enhancement New feature or request label Mar 29, 2026
bmuschko and others added 2 commits March 29, 2026 16:54
Use UsesMethod precondition instead of UsesType for more precise
matching, static imports for Collections methods, emptyList() for
no-arg calls, and ListUtils.mapFirst to simplify arg remapping.
Copy link
Copy Markdown
Member

@timtebeek timtebeek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Neat to see type information mostly retained by looking up the method invoked.

@github-project-automation github-project-automation Bot moved this from In Progress to Ready to Review in OpenRewrite Mar 30, 2026
@timtebeek timtebeek merged commit 04c94f8 into main Mar 30, 2026
1 check passed
@timtebeek timtebeek deleted the bmuschko/fix-donothing-when branch March 30, 2026 09:59
@github-project-automation github-project-automation Bot moved this from Ready to Review to Done in OpenRewrite Mar 30, 2026
mergify Bot added a commit to robfrank/linklift that referenced this pull request May 3, 2026
…rom 3.30.0 to 3.35.2 [skip ci]

Bumps [org.openrewrite.recipe:rewrite-testing-frameworks](https://github.com/openrewrite/rewrite-testing-frameworks) from 3.30.0 to 3.35.2.
Release notes

*Sourced from [org.openrewrite.recipe:rewrite-testing-frameworks's releases](https://github.com/openrewrite/rewrite-testing-frameworks/releases).*

> 3.35.2
> ------
>
> What's Changed
> --------------
>
> * OpenRewrite v8.81.0: <openrewrite/rewrite@v8.81.0...main>
>
> **Full Changelog**: <openrewrite/rewrite-testing-frameworks@v3.35.1...v3.35.2>
>
> 3.35.1
> ------
>
> What's Changed
> --------------
>
> * Fix `UpdateMockWebServerDispatcher` classpath lookup by [`@​knutwannheden`](https://github.com/knutwannheden) in [openrewrite/rewrite-testing-frameworks#975](https://redirect.github.com/openrewrite/rewrite-testing-frameworks/pull/975)
>
> **Full Changelog**: <openrewrite/rewrite-testing-frameworks@v3.35.0...v3.35.1>
>
> 3.35.0
> ------
>
> What's Changed
> --------------
>
> * Skip FieldAccess arguments in AssertThrowsOnLastStatement by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-testing-frameworks#965](https://redirect.github.com/openrewrite/rewrite-testing-frameworks/pull/965)
> * Add MigrateToKafkaNative recipe for Testcontainers ([#568](https://redirect.github.com/openrewrite/rewrite-testing-frameworks/issues/568)) by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-testing-frameworks#967](https://redirect.github.com/openrewrite/rewrite-testing-frameworks/pull/967)
> * Fix ClassCastException in RemoveTimesZeroAndOne by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-testing-frameworks#969](https://redirect.github.com/openrewrite/rewrite-testing-frameworks/pull/969)
> * Fix RemoveTryCatchFailBlocks failing when removing blocks within lambda expressions by [`@​sambsnyd`](https://github.com/sambsnyd) in [openrewrite/rewrite-testing-frameworks#970](https://redirect.github.com/openrewrite/rewrite-testing-frameworks/pull/970)
> * Use KotlinTypeUtils.isKotlinUnit to match Kotlin void return types by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-testing-frameworks#971](https://redirect.github.com/openrewrite/rewrite-testing-frameworks/pull/971)
> * Fix MockWebServer Dispatcher return type and RecordedRequest renames by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-testing-frameworks#972](https://redirect.github.com/openrewrite/rewrite-testing-frameworks/pull/972)
> * Add [`@​MockitoSettings`](https://github.com/MockitoSettings)(strictness = Strictness.WARN) for Mockito migration by [`@​bmuschko`](https://github.com/bmuschko) in [openrewrite/rewrite-testing-frameworks#966](https://redirect.github.com/openrewrite/rewrite-testing-frameworks/pull/966)
>
> **Full Changelog**: <openrewrite/rewrite-testing-frameworks@v3.34.0...v3.35.0>
>
> 3.34.0
> ------
>
> What's Changed
> --------------
>
> * Remove empty if blocks after initMocks/openMocks cleanup by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-testing-frameworks#952](https://redirect.github.com/openrewrite/rewrite-testing-frameworks/pull/952)
> * Extend SimplifyMockitoVerifyWhenGiven for more S6068 cases by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-testing-frameworks#953](https://redirect.github.com/openrewrite/rewrite-testing-frameworks/pull/953)
> * Add RemoveDoNothingForDefaultMocks to Mockito1to3Migration by [`@​bmuschko`](https://github.com/bmuschko) in [openrewrite/rewrite-testing-frameworks#956](https://redirect.github.com/openrewrite/rewrite-testing-frameworks/pull/956)
> * Skip private method stubbing in PowerMockitoDoStubbingToMockito by [`@​bmuschko`](https://github.com/bmuschko) in [openrewrite/rewrite-testing-frameworks#957](https://redirect.github.com/openrewrite/rewrite-testing-frameworks/pull/957)
> * Fix AssertToAssertionsTest expectations for static imports by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-testing-frameworks#958](https://redirect.github.com/openrewrite/rewrite-testing-frameworks/pull/958)
> * Add ThenThrowCheckedExceptionToRuntimeException recipe by [`@​bmuschko`](https://github.com/bmuschko) in [openrewrite/rewrite-testing-frameworks#954](https://redirect.github.com/openrewrite/rewrite-testing-frameworks/pull/954)
> * Fix AnyToNullable removing any() import when untyped any() still used by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-testing-frameworks#960](https://redirect.github.com/openrewrite/rewrite-testing-frameworks/pull/960)
> * Rename implicit [`@​MethodSource`](https://github.com/MethodSource) methods in RemoveTestPrefix by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-testing-frameworks#961](https://redirect.github.com/openrewrite/rewrite-testing-frameworks/pull/961)
> * Add UpgradeWiremockDependencyVersion recipe by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-testing-frameworks#963](https://redirect.github.com/openrewrite/rewrite-testing-frameworks/pull/963)
> * Support intermediate methods in SimplifyChainedAssertJAssertion by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-testing-frameworks#962](https://redirect.github.com/openrewrite/rewrite-testing-frameworks/pull/962)
> * Add MigrateToOracleFree recipe for Testcontainers by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-testing-frameworks#964](https://redirect.github.com/openrewrite/rewrite-testing-frameworks/pull/964)
>
> **Full Changelog**: <openrewrite/rewrite-testing-frameworks@v3.33.0...v3.34.0>
>
> 3.33.0
> ------
>
> What's Changed
> --------------
>
> * OpenRewrite recipe best practices by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-testing-frameworks#949](https://redirect.github.com/openrewrite/rewrite-testing-frameworks/pull/949)
> * Add RemoveDoNothingForDefaultMocks recipe by [`@​bmuschko`](https://github.com/bmuschko) in [openrewrite/rewrite-testing-frameworks#947](https://redirect.github.com/openrewrite/rewrite-testing-frameworks/pull/947)
> * Add PowerMockitoDoStubbingToMockito recipe by [`@​bmuschko`](https://github.com/bmuschko) in [openrewrite/rewrite-testing-frameworks#948](https://redirect.github.com/openrewrite/rewrite-testing-frameworks/pull/948)
> * Remove [`@​SuppressStaticInitializationFor`](https://github.com/SuppressStaticInitializationFor) during PowerMockito migration by [`@​bmuschko`](https://github.com/bmuschko) in [openrewrite/rewrite-testing-frameworks#946](https://redirect.github.com/openrewrite/rewrite-testing-frameworks/pull/946)

... (truncated)


Commits

* [`1302589`](openrewrite/rewrite-testing-frameworks@1302589) Make declarative recipes singletons
* [`92de89a`](openrewrite/rewrite-testing-frameworks@92de89a) Fix UpdateMockWebServerDispatcher classpath lookup ([#975](https://redirect.github.com/openrewrite/rewrite-testing-frameworks/issues/975))
* [`64260b8`](openrewrite/rewrite-testing-frameworks@64260b8) Extract documentation examples from tests
* [`e09430b`](openrewrite/rewrite-testing-frameworks@e09430b) Add [`@​MockitoSettings`](https://github.com/MockitoSettings)(strictness = Strictness.WARN) for Mockito migration ([#966](https://redirect.github.com/openrewrite/rewrite-testing-frameworks/issues/966))
* [`f041383`](openrewrite/rewrite-testing-frameworks@f041383) Fix MockWebServer Dispatcher return type and RecordedRequest renames ([#972](https://redirect.github.com/openrewrite/rewrite-testing-frameworks/issues/972))
* [`13f0a17`](openrewrite/rewrite-testing-frameworks@13f0a17) OpenRewrite recipe best practices
* [`83d1f44`](openrewrite/rewrite-testing-frameworks@83d1f44) Use KotlinTypeUtils.isKotlinUnit to match Kotlin void return types ([#971](https://redirect.github.com/openrewrite/rewrite-testing-frameworks/issues/971))
* [`675b57d`](openrewrite/rewrite-testing-frameworks@675b57d) Fix RemoveTryCatchFailBlocks failing when removing blocks within lambda expre...
* [`ea72478`](openrewrite/rewrite-testing-frameworks@ea72478) Fix ClassCastException in RemoveTimesZeroAndOne ([#969](https://redirect.github.com/openrewrite/rewrite-testing-frameworks/issues/969))
* [`58bcc12`](openrewrite/rewrite-testing-frameworks@58bcc12) Add MigrateToKafkaNative recipe for Testcontainers ([#568](https://redirect.github.com/openrewrite/rewrite-testing-frameworks/issues/568)) ([#967](https://redirect.github.com/openrewrite/rewrite-testing-frameworks/issues/967))
* Additional commits viewable in [compare view](openrewrite/rewrite-testing-frameworks@v3.30.0...v3.35.2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

2 participants