Skip to content

Tighten PowerMockitoMockStaticToMockito precondition to actual PowerMockito usage #980

@timtebeek

Description

@timtebeek

Problem

PowerMockitoMockStaticToMockito's precondition is currently:

Preconditions.and(
    Preconditions.or(
        new UsesType<>(\"org.powermock..*\", false),
        new UsesType<>(\"org.mockito..*\", false)
    ),
    Preconditions.not(new KotlinFileChecker<>())
)
  • The org.mockito..* clause means any test file that imports something from org.mockito.* qualifies — including files with no PowerMockito anywhere. Since the recipe is registered transitively under MockitoBestPracticesMockito1to5MigrationMockito1to4MigrationMockito1to3Migration, every Mockito-only test ends up running PowerMockito-to-Mockito conversion logic. That's how we hit Preserve mockStatic when returned from helper methods #979 (helper returning mockStatic(...)) on a project with no PowerMockito at all.

Suggested fix

Restrict to actual PowerMockito signal:

Preconditions.and(
    Preconditions.or(
        new UsesType<>(\"org.powermock..*\", false),
        new UsesType<>(\"org.powermock.core.classloader.annotations.PrepareForTest\", false)
    ),
    Preconditions.not(new KotlinFileChecker<>())
)

(The second clause is redundant under the first, but spelling it out makes intent clear if someone later relaxes the wildcard.)

Risk

Tightening the precondition will skip files that don't import PowerMockito but happened to be "fixed up" by this recipe's other branches (e.g. the bare-mockStatic removal, the dynamic-when rewrite). Need to audit which of those branches are useful for non-PowerMockito files and either move them to a separate recipe or accept the scope reduction.

Related

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    Status
    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions