Skip to content

Refactor PowerMockito recipes for clarity and single responsibility#943

Merged
timtebeek merged 5 commits intomainfrom
tim/improve-replace-powermockito
Mar 26, 2026
Merged

Refactor PowerMockito recipes for clarity and single responsibility#943
timtebeek merged 5 commits intomainfrom
tim/improve-replace-powermockito

Conversation

@timtebeek
Copy link
Copy Markdown
Member

@timtebeek timtebeek commented Mar 26, 2026

Summary

  • Extract TestFramework enum to replace mutable visitor state with an immutable, cursor-stored value
  • Extract RemovePowerMockClassExtensions into its own recipe, added to declarative YAML recipeList
  • Fold @RunWith(PowerMockRunner.class) removal into PowerMockRunnerDelegateToRunWith
  • Use service(AnnotationService.class) in PowerMockRunnerDelegateToRunWith instead of manual annotation matching
  • Consolidate duplicate isStaticMockAlreadyClosed/isStaticMockAlreadyOpened into single hasMatchingInvocation method
  • Restructure visitor methods with early returns for clarity

Test plan

  • All existing PowerMockitoMockStaticToMockitoTest tests pass
  • All existing PowerMockRunnerDelegateToRunWithTest tests pass (including new no-delegate case)
  • New RemovePowerMockClassExtensionsTest tests pass

…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
@timtebeek timtebeek changed the title Refactor PowerMockitoMockStaticToMockito for clarity and single responsibility Refactor PowerMockito recipes for clarity and single responsibility Mar 26, 2026
@timtebeek timtebeek requested review from bmuschko and Copilot March 26, 2026 10:33

This comment was marked as outdated.

@timtebeek timtebeek moved this from In Progress to Ready to Review in OpenRewrite Mar 26, 2026
import org.openrewrite.java.search.FindAnnotations;
import org.openrewrite.java.tree.J;

enum TestFramework {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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("() -> #{}.#{}(#{})")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nice shortcut to make this more generic.

"""
import org.junit.Test;


Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This seems to be adding an extra newline.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Thanks! I think that's a known issue of removing an annotation that well have to fix outside this repository.

@timtebeek timtebeek merged commit 43acae3 into main Mar 26, 2026
1 check passed
@timtebeek timtebeek deleted the tim/improve-replace-powermockito branch March 26, 2026 16:36
@github-project-automation github-project-automation Bot moved this from Ready to Review to Done in OpenRewrite Mar 26, 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.

3 participants