rewrite-testing-frameworks icon indicating copy to clipboard operation
rewrite-testing-frameworks copied to clipboard

rewrite-testing-frameworks: AnyToNullable too greedily removes the import to any

Open Laurens-W opened this issue 2 years ago • 1 comments

What version of OpenRewrite are you using?

I am using

  • OpenRewrite v. which ever is provided by the maven plugin
  • Maven/Gradle plugin v5.0.0
  • rewrite-testing-frameworks v2.0.0

How are you running OpenRewrite?

I am using the Maven plugin from the commandline within a Jenkins job:

mvn org.openrewrite.maven:rewrite-maven-plugin:5.0.0:run -Drewrite.activeRecipes=redacted -Drewrite.recipeArtifactCoordinates=org.openrewrite.recipe:rewrite-java-security:LATEST,org.openrewrite.recipe:rewrite-kubernetes:LATEST,org.openrewrite.recipe:rewrite-logging-frameworks:LATEST,org.openrewrite.recipe:rewrite-migrate-java:LATEST,org.openrewrite.recipe:rewrite-spring:LATEST,org.openrewrite.recipe:rewrite-testing-frameworks:LATEST -Drewrite.exclusions=**api**.yaml

What is the smallest, simplest way to reproduce the problem?

Adding the following testcase to the org/openrewrite/java/testing/mockito/AnyToNullableTest.java and running it will show it fails

    @Test
    void shouldNotReplaceUntypedAny() {
        //language=java
        rewriteRun(
          //language=xml
          pomXml("""
            <project>
                <modelVersion>4.0.0</modelVersion>
                <groupId>com.example</groupId>
                <artifactId>foo</artifactId>
                <version>1.0.0</version>
                <dependencies>
                    <dependency>
                        <groupId>org.mockito</groupId>
                        <artifactId>mockito-all</artifactId>
                        <version>1.10.19</version>
                    </dependency>
                </dependencies>
            </project>
            """),
          //language=java
          java("""
            class Example {
                String greet(Object obj, Object obj2) {
                    return "Hello " + obj + obj2;
                }
            }
            """),
          //language=java
          java(
            """
              import static org.mockito.Mockito.mock;
              import static org.mockito.Mockito.when;
              import static org.mockito.Mockito.any;

              class MyTest {
                   void test() {
                      Example example = mock(Example.class);
                      when(example.greet(any(Object.class), any())).thenReturn("Hello world");
                   }
              }
              """,
            """
              import static org.mockito.ArgumentMatchers.nullable;
              import static org.mockito.Mockito.mock;
              import static org.mockito.Mockito.when;
              import static org.mockito.Mockito.any;
                            
              class MyTest {
                   void test() {
                      Example example = mock(Example.class);
                      when(example.greet(nullable(Object.class), any())).thenReturn("Hello world");
                   }
              }
              """
          )
        );
    }

What did you expect to see?

The any import should not be removed as it is required by the untyped any(), if anything it should be replaced with the ArgumentMatchers.any

What did you see instead?

The import is removed as part of the AnyToNullable recipe

What is the full stack trace of any errors you encountered?

stacktrace output here

Are you interested in contributing a fix to OpenRewrite?

Laurens-W avatar Jun 13 '23 12:06 Laurens-W

This issue is quite likely linked to https://github.com/openrewrite/rewrite-testing-frameworks/pull/336 and https://github.com/openrewrite/rewrite/issues/3111.

knutwannheden avatar Jun 13 '23 13:06 knutwannheden