Refactor PowerMockito recipes for clarity and single responsibility#943
Merged
Refactor PowerMockito recipes for clarity and single responsibility#943
Conversation
…nsibility Extract independent responsibilities from the monolithic 557-line recipe: - Extract `RemovePowerMockClassExtensions` recipe for removing `extends PowerMockConfiguration` and `extends PowerMockTestCase` - Fold `@RunWith(PowerMockRunner.class)` removal (with and without delegate) into `PowerMockRunnerDelegateToRunWith` - Replace 8 mutable instance fields with a `TestFramework` enum - Add early returns throughout to reduce nesting and improve readability
- Consolidate isStaticMockAlreadyClosed/isStaticMockAlreadyOpened into single hasMatchingInvocation method parameterized by MethodMatcher - Restructure visitMethodInvocation with early returns for clarity - Fix double getTestGroupsAsString() call in maybeAddTearDownMethodBody - Simplify mockStatic tracking to use direct list access instead of Optional stream
- Use service(AnnotationService.class) instead of manual stream check - Extract findDelegateRunnerArg method to avoid finalDelegateRunnerArg - Move shared maybeRemoveImport(POWER_MOCK_RUNNER) above if/else so each branch can return immediately
bmuschko
approved these changes
Mar 26, 2026
| import org.openrewrite.java.search.FindAnnotations; | ||
| import org.openrewrite.java.tree.J; | ||
|
|
||
| enum TestFramework { |
Contributor
There was a problem hiding this comment.
I like this abstraction. I didn't have a chance to look through all of the recipes in this space but I would image that this logic is asked for all the time.
| J.Literal calledMethod = (J.Literal) arguments.get(0); | ||
| arguments.remove(0); | ||
| String stringOfArguments = arguments.stream().map(Object::toString).collect(joining(",")); | ||
| method = JavaTemplate.builder("() -> #{}.#{}(#{})") |
Contributor
There was a problem hiding this comment.
Nice shortcut to make this more generic.
| """ | ||
| import org.junit.Test; | ||
|
|
||
|
|
Contributor
There was a problem hiding this comment.
This seems to be adding an extra newline.
Member
Author
There was a problem hiding this comment.
Thanks! I think that's a known issue of removing an annotation that well have to fix outside this repository.
…nnerDelegateToRunWith.java
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
TestFrameworkenum to replace mutable visitor state with an immutable, cursor-stored valueRemovePowerMockClassExtensionsinto its own recipe, added to declarative YAMLrecipeList@RunWith(PowerMockRunner.class)removal intoPowerMockRunnerDelegateToRunWithservice(AnnotationService.class)inPowerMockRunnerDelegateToRunWithinstead of manual annotation matchingisStaticMockAlreadyClosed/isStaticMockAlreadyOpenedinto singlehasMatchingInvocationmethodTest plan
PowerMockitoMockStaticToMockitoTesttests passPowerMockRunnerDelegateToRunWithTesttests pass (including new no-delegate case)RemovePowerMockClassExtensionsTesttests pass