openrewrite / rewrite-testing-frameworks

OpenRewrite recipes that perform common Java testing migration tasks.
Apache License 2.0
77 stars 73 forks source link

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

Open Laurens-W opened 1 year ago

Laurens-W commented 1 year ago

What version of OpenRewrite are you using?

I am using

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?

knutwannheden commented 1 year ago

This issue is quite likely linked to