Skip to content

ChangeType doesn't use fully qualified names in import statements #7

@sambsnyd

Description

@sambsnyd

Description/ Observed Behavior

When attempting to use the ChangeType recipe to swap org.mockito.Matchers (used in mockito 1) with org.mockito.ArgumentMatchers (used in mockito 2+) I observed that the import statements in the resulting code are missing the "org.mockito" bit.

Here's an example of the visitor:

@AutoConfigure
public class ReplaceMatchersWithArgumentMatchers extends JavaRefactorVisitor {
    private final ChangeType changeAny = new ChangeType();

    public ReplaceMatchersWithArgumentMatchers() {
        changeAny.setType("org.mockito.Matchers");
        changeAny.setTargetType("org.mockito.ArgumentMatchers");
    }

    @Override
    public J visitCompilationUnit(J.CompilationUnit cu) {
        return changeAny.visitCompilationUnit(cu);
    }
}

Operating on code with imports like these:

import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyBoolean;

Produces these invalid import statements:

import static ArgumentMatchers.any;
import static ArgumentMatchers.anyBoolean;

This isn't specific to the imports being static, all imports are affected.

Desired Behavior

This transformation should produce import statements like:

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyBoolean;

Repro Steps

  1. Clone the rewrite-spring project
  2. Switch to the "mockito" branch
  3. Run the tests in ReplaceMatchersWithArgumentMatchersTest.kt

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions