Skip to content

Add @MockitoSettings(strictness = Strictness.WARN) for Mockito migration#966

Draft
bmuschko wants to merge 2 commits intomainfrom
bmuschko/mockito-settings-warn
Draft

Add @MockitoSettings(strictness = Strictness.WARN) for Mockito migration#966
bmuschko wants to merge 2 commits intomainfrom
bmuschko/mockito-settings-warn

Conversation

@bmuschko
Copy link
Copy Markdown
Contributor

@bmuschko bmuschko commented Apr 8, 2026

Context

After migrating from Mockito 1.x, tests fail with UnnecessaryStubbingException because @ExtendWith(MockitoExtension.class) defaults to Strictness.STRICT_STUBS, whereas Mockito 1.x/2.x silently ignored unused stubbings (Strictness.WARN). Detecting which stubbings are truly unnecessary requires runtime analysis, so the practical fix is to add @MockitoSettings(strictness = Strictness.WARN) at the class level to preserve the original behavior.

The existing MockitoJUnitToMockitoExtension recipe already handled this when converting a MockitoRule, but had two gaps:

  1. When @ExtendWith(MockitoExtension.class) was already present alongside a MockitoRule, the recipe removed the rule but did not add @MockitoSettings, silently changing strictness from WARN to STRICT_STUBS.
  2. Classes that were already fully migrated (no MockitoRule remaining, just @ExtendWith) were not covered at all — no existing recipe would add the missing @MockitoSettings.

Changes

  1. MockitoJUnitToMockitoExtension: Decoupled the @ExtendWith and @MockitoSettings logic so that @MockitoSettings is now added even when @ExtendWith is already present (fixes gap 1).

  2. New AddMockitoSettingsWithWarnStrictness recipe: Adds @MockitoSettings(strictness = Strictness.WARN) to classes that have @ExtendWith(MockitoExtension.class) but no @MockitoSettings annotation. This covers classes already migrated without a MockitoRule to convert (fixes gap 2). Added to the Mockito1to3Migration chain.

…o migration

Addresses the UnnecessaryStubbingException issue that occurs after migrating
from Mockito 1.x, where unused stubbings were silently ignored but now fail
under MockitoExtension's default STRICT_STUBS strictness.

Two changes:

1. MockitoJUnitToMockitoExtension now adds @MockitoSettings even when
   @ExtendWith(MockitoExtension.class) is already present, preserving the
   original WARN strictness from the MockitoRule being removed.

2. New AddMockitoSettingsWithWarnStrictness recipe targets classes that
   already have @ExtendWith(MockitoExtension.class) but no MockitoRule
   to convert and no @MockitoSettings — the gap not covered by the
   existing recipe.
@github-project-automation github-project-automation bot moved this to In Progress in OpenRewrite Apr 8, 2026
@bmuschko bmuschko added the enhancement New feature or request label Apr 8, 2026
- org.openrewrite.java.testing.mockito.ReplacePowerMockito
- org.openrewrite.java.testing.junit5.MockitoJUnitToMockitoExtension
- org.openrewrite.java.testing.mockito.AddMockitoExtensionIfAnnotationsUsed
- org.openrewrite.java.testing.mockito.AddMockitoSettingsWithWarnStrictness
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think we'd want to guard this with a precondition to only apply when the module has a dependency on a pre-version-3 of Mockito right?

What I'm trying to avoid is that folks who never used Mockito 1, 2 or 3, but say started at 4, then run our recipe to upgrade to 5, and suddenly see MockitoSettings restore an old default they had never used to begin with, reducing the strictness of their Mockito usage.

Would you agree with the above scenario & assessment?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Makes sense. I think we have two options here:

  1. I made the recipe a standalone recipe so that end users have to run it on-demand. It will only be applicable to projects that are interested in Mockito 1.x/2.x migrations.
  2. I am not a fan of this option but here it is. We could add a version parameter to the Java recipe (e.g. a String maxMockitoVersion option) and implement a scanning precondition to check the dependency version programmatically.

WDYT?

@bmuschko
Copy link
Copy Markdown
Contributor Author

bmuschko commented Apr 8, 2026

I'll make this a draft for now. It's still failing on CI anyway.

@bmuschko bmuschko marked this pull request as draft April 8, 2026 22:36
MockitoBestPractices includes RemoveAnnotation for @MockitoSettings(WARN),
which conflicts with our recipe when both run in the same composite chain
(e.g., JMockitToMockito → MockitoBestPractices → Mockito1to5Migration).
Restore UsesType precondition and remove from Mockito1to3Migration chain.
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

Status: In Progress

Development

Successfully merging this pull request may close these issues.

2 participants